Appending char to string (char array)

This is a discussion on Appending char to string (char array) within the C Programming forums, part of the General Programming Boards category; Hi there.. I have searched all over the net for this little feature, but cannot find it anywhere.. How does ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    46

    Appending char to string (char array)

    Hi there..

    I have searched all over the net for this little feature, but cannot find it anywhere.. How does I append a character to a string.. It sounds very simple, but can't find help to do so anywhere..

    I tried to use a string to do this this as follows, but it does not do it quite well, and looks very stupid:

    char strTMP[1];
    char testint2[] = "BLA BLA BLA ";
    strTMP[0] = c[i]; // returns a char
    strcat(testint2,strTMP);

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    1. you should have enough room in your buffer to append something, currently your testint2 buffer size is enough only to store the "BLA BLA BLA " string...

    2. easiest way to append char - using simple assignment
    Code:
    char res[10]="start ";
    size_t len = strlen(res);
    res[len++] = 'A'; /* we overwriting the null-character with another one */
    res[len] = '\0'; /* to make the string null-terminated again */
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by vart View Post
    1. you should have enough room in your buffer to append something, currently your testint2 buffer size is enough only to store the "BLA BLA BLA " string...

    2. easiest way to append char - using simple assignment
    Code:
    char res[10]="start ";
    size_t len = strlen(res);
    res[len++] = 'A'; /* we overwriting the null-character with another one */
    res[len] = '\0'; /* to make the string null-terminated again */
    I was aware of that problem thats why i used strcat, which automatically allocates extra space..
    I think I have tried this but then it complains about precisely "char res[10]="start ";". Something about that a value cannot be assigned this way when the length is set.. But I'll try again then and see if it should be possible..

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Make sure you have enough room to do it. Otherwise, you'll overwrite some other memory with your new '\0'.

    You don't really need a function to do this per se, but if you did want one:

    Code:
    char *appendchar(char *szString, size_t strsize, char c)
    {
    	size_t len = strlen(szString);
    	if((len+1) < strsize)
    	{
    		szString[len++] = c;
    		szString[len] = '\0';
    		return szString;
    	}
    	return NULL;
    }
    Example usage:

    Code:
    char szBuffer[BUFSIZ];
    
    /* Note: szBuffer must have a '\0' char inside of it before this function is called. */
    
    ...
    
    appendchar(szBuffer,sizeof(szBuffer),'x');

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    46

    Smile

    Thank you both... It seems to work..

    Why did it give me an error message on the specified "char res[10]="start ";" earlier.. Well, now it seems to work..

    Thanks again...

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    46
    And I have thought about the null terminating character inside the strings, but did not think it was nessasary when I just use the only character avaliable in the chararray[1], but maybe to secure I should have used chararray[2] then..

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by MacGyver View Post
    Make sure you have enough room to do it. Otherwise, you'll overwrite some other memory with your new '\0'.

    You don't really need a function to do this per se, but if you did want one:

    Code:
    char *appendchar(char *szString, size_t strsize, char c)
    {
    	size_t len = strlen(szString);
    	if((len+1) < strsize)
    	{
    		szString[len++] = c;
    		szString[len] = '\0';
    		return szString;
    	}
    	return NULL;
    }
    Example usage:

    Code:
    char szBuffer[BUFSIZ];
    
    /* Note: szBuffer must have a '\0' char inside of it before this function is called. */
    
    ...
    
    appendchar(szBuffer,sizeof(szBuffer),'x');
    And the usage of this method should be stored in a char[] right..? like
    char res[20];
    res = appendchar(szBuffer,sizeof(szBuffer),'x');

    but then I get the message.. Lvalue required in function find... what does this mean..? I'm not that strong to pointers.. :S

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Not quite.

    If your char array is declared this way:

    Code:
    char res[20];
    And let's say you wanted to append the character 'C', then the usage would be like this:

    Code:
    appendchar(res,sizeof(res),'C');
    The return value is just mainly for checking if the function was successful or not.

    For example:

    Code:
    ...
    if(appendchar(res,sizeof(res),'C'))
    {
    	/* The character was appended to res*/
    }
    else
    {
    	/* The character was not appended because there was not enough room in res. */
    }

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    46
    Ahh, I see.. The pointer to the method saves automatically in the sting-value given to the method (*szString). It saves in the same string because it is given as a pointer, right..?

    As said I'm not that good to pointers yet.. :S

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes, correct.

    When a pointer is passed, the data it points to can be altered by the function. Since a C-string is just a pointer to the first element of an array by the time it gets to a function, any character in that string can be altered by the function and the changes will be reflected in the calling function.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    46
    Thank you very much.. Now I know a little about incomming pointers to a method as well..

  12. #12
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    i have some questions about that function:
    when i apply to this code:
    Code:
    int main()
    {
       clrscr();
       Stack *stack=new Stack(100);
       char x[100];
       printf("Input Equation: ");
       fgets(x);
       char dest[100];
    
       for(int i=0;i<strlen(x);i++)
       {
    
    	if(isdigit(x[i]))
    	{
    		appendchar(dest, sizeof(dest), x[i]);
    
    	}
    	else if((x[i]=='+')||(x[i]=='-')||(x[i]=='*')||(x[i]=='/'))
    	{
    		stack->push(x[i]);
    	}
    	else if(x[i]==')')
    	{
    		appendchar(dest, sizeof(dest), stack->pop());
    	}
    
    
       }
       printf("&#37;s", dest);
       printf("\n\nPress any key to exit");
        return 0;
    
    }
    
    char *appendchar(char *s, size_t strsize, char c)
    {
    	size_t len=strlen(s);
    	if((len+1) < strsize)
    	{
    		s[len++]=c;
    		s[len]='\0';
    		return s;
    	}
    	return NULL;
    }
    what will be the result for this code?
    Last edited by blacksnake; 04-15-2008 at 07:01 AM.
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by blacksnake View Post
    what will be the result for this code?
    Compilation errors, obviously. This isn't C
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by sniper83 View Post
    I was aware of that problem thats why i used strcat, which automatically allocates extra space..
    The strcat function does not allocate any extra space.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by blacksnake View Post
    what will be the result for this code?
    You were told to correct that code before. Don't use old code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21