Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:52:26+00:00 2026-06-18T15:52:26+00:00

How do you append a number to an LPCTSTR? A library I am using

  • 0

How do you append a number to an LPCTSTR? A library I am using takes an LPCTSTR for a com port address. I know how to do this with char* , but not with a LPCTSTR. This is what I want to do (assuming sprintf as is worked with these, which I know it doesn’t)

LPCTSTR PortString;
int ComPortNumber;

sprintf(PortString,"COM%d",ComPortNumber);

Such that when that is done, the LPCTSTR PortString, would contain “COM9” if 9 was stored in the ComPortNumber integer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:52:28+00:00Added an answer on June 18, 2026 at 3:52 pm

    An LPCTSTR is a pointer to a TCHAR – in other words a pointer to a string. In the code snippet you provide, it points to some random area of memory and running the code is undefined behavior since you are accessing some random area of memory by dereferencing an uninitialized pointer.

    Try this code instead:

    TCHAR PortString[64];
    int ComPortNumber;
    
    /* assign some value to ComPortNumber here */
    
    _sntprintf_s(
        PortString,                       // The buffer for the output
        sizeof(PortString)/sizeof(TCHAR), // The number of TCHARs in the buffer
        _TRUNCATE,                        // How to handle overflows
        _T("COM%d"),                      // The format string
        ComPortNumber);                   // And the port number, finally!
    

    I used the T variants of the calls and types to ensure that you code would compile in both ANSI/MBCS and UNICODE modes, and the new “secure” variant of _sntprintf to help reduce the chance of buffer overrruns.

    In real production code you should check the return address from the _sntprintf_s call for errors.

    One last point: be careful to not return PortString to whoever calls this function, as it is stack based, and when this function exits, the buffer will disappear. If you do this your program will crash while you’re debugging/testing if you’re lucky. If you’re not lucky, it may appear to work correctly but it will be a ticking timebomb.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to append unlimited number of HTML elements with data given as comment.
I have a list like: list = [[1,2,3],[4,5,6],[7,8,9]] I want to append a number
I am still a beginner but I want to write a character-recognition-program. This program
I want to append a random number or a timestamp at the end of
I have 10 form fields and I want to append a number next to
I figured out how to clone my form rows and append an incriminting number
I would like to count the number of ('.child') in each container and append
I have a javascript loop and want to highlight some numbers for(i=0;i<=100;i++){ $(#mydiv).append(<span>+i+</span>); }
Initialize an array of arrays: M = [[]]*(24*60/5) Append the number 2 to the
I'd like to know how to keep adding number to a varible. For instance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.