Thread: Append an integer to a char * as a count

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    7

    Append an integer to a char * as a count

    I am trying to keep a count on a variable name stored within a structure as char*'s. They are of the same field and I do not know how many there will be so I would like to keep a standard name and append the count.

    So say I have a variable name such as "desk", but as I have many of these said desks so I would like to call them "desk1", "desk2", "desk3" and so forth. Any recommendations on how I could do this?

    Also since this is somewhat relevant is there an easy way to convert from an integer to a string, something that would work like atoi() in reverse? I wouldn't mind writing a method to do so myself but haven't a clue as to how.

    Thank you for your help!
    Last edited by googol; 04-15-2014 at 05:58 PM.

  2. #2
    Registered User MilleniumFalcon's Avatar
    Join Date
    Feb 2014
    Posts
    33
    Quote Originally Posted by googol View Post
    Also since this is somewhat relevant is there an easy way to convert from an integer to a string, something that would work like atoi() in reverse? I wouldn't mind writing a method to do so myself but haven't a clue as to how.
    You can use sprintf() for conversions to a string. The call could be implemented as this,

    Code:
    sprintf( myString, "%d", myInt );
    Quote Originally Posted by googol View Post
    I am trying to keep a count on a variable name stored within a structure as char*'s. They are of the same field and I do not know how many there will be so I would like to keep a standard name and append the count.

    So say I have a variable name such as "desk", but as I have many of these said desks so I would like to call them "desk1", "desk2", "desk3" and so forth. Any recommendations on how I could do this?
    I'm not sure I understand what you're asking for. Are you wanting to find a way to have multiple fields with similar names and use them without the full name? You could use macros to paste tokens together for access such as that. Or do you literally want to store names of all your variables? You could use a char ** for an array of strings, and use a separate variable to keep track of the amount of strings.
    Last edited by MilleniumFalcon; 04-15-2014 at 06:16 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    MilleniumFalcon has your solution for "int to string".

    Your description of what you really need for "desk1", "desk2", ... is a little unclear. Perhaps you should look into arrays: Arrays in C - Cprogramming.com. If that's not what you're looking for, perhaps you could show us some code, or pseudo code to better illustrate your problem.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    7
    Thank you for the replies. I have not used macros a great deal in the past but I will certainly look into them. Let me see if I can clarify a little more though. I just am curious to know if there is a way I could concatenate an integer onto a string while still keeping the string format (by string I mean char[]). I have had many instances lately where this would have helped so its for more than this example exclusively. This is what I'm after, although I know this doesn't work.

    int n = 99;
    char * str = "vote";
    char * newstr = strcat(str, n);
    I do not know how to convert the n to a string in order to accomplish this.

    Thanks!

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by googol View Post
    I do not know how to convert the n to a string in order to accomplish this.
    A standard way is using sprintf sprintf - C++ Reference

    A NON-standard way is using itoa itoa - C++ Reference

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So, in order for you to actually write to a string, you have to have writable memory. A string literal (a.k.a. string constant) is not writable -- it's constant, unchanging. Thus, you can't do
    Code:
    char *foo = "some stuff";
    strcat(foo, "more stuff");
    What you do is declare an array that is large enough to hold the largest string you need (+1 for the terminating null character), and use sprintf as Tim suggested:
    Code:
    char my_string[17];
    sprintf(my_string, "vote%d", 42);
    But I would be remiss if I didn't mention two excellent improvements to the above code: defining a constant for the string length, and using snprintf (documentation). The 'n' is the version that limits how much it writes into your string so you don't overflow it.
    Code:
    #define MAX_STR_LEN 100
    ...
    // in a function because global variables are evil
    char my_string[MAX_STR_LEN + 1];  // +1 for the null terminator
    snprintf(my_string, sizeof(my_string), "vote%d", 42);
    Bonus: notice that I used sizeof(my_string) for the size of my_string, instead of MAX_STR_LEN. Imagine you wanted to change my_string to be of size SOME_OTHER_CONSTANT. If I used MAX_STR_LEN in the snprintf call, I would have to remember to change it -- and I might forget, leaving open the possibility of an overflow. Using sizeof will always give the right size.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-24-2012, 11:28 PM
  2. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 08:16 AM
  3. Append certain number of characters to char[]?
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-16-2006, 08:39 PM
  4. Count the number of element in 2D char array
    By alice in forum C Programming
    Replies: 2
    Last Post: 04-24-2004, 12:51 AM
  5. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 04:16 PM