append char to char*

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

append char to char*

 
0
  #1
Jun 12th, 2007
Hi I would like to create a function that appends a character to a character array.. this is what I have got so far..

  1. char * appendCharToCharArray(char * array, char a)
  2. {
  3. char * ret = (char*)malloc(sizeof(array) + 1 + 1);
  4. strcpy(ret,array);
  5. ret[strlen(ret)] = a;
  6. ret[sizeof(ret)] = '\0';
  7. return ret;
  8. }

this seems to work but only for up to 4 letter words somehow.

I thought maybe I could do it using a string object to concatenate and then returning it as a characted array using stringobject.c_str(); but I would prefer not to do this unless I have to.

Any help is greatly appreciated.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: append char to char*

 
0
  #2
Jun 12th, 2007
>this seems to work but only for up to 4 letter words somehow.
It seems the size of a pointer is four bytes on your system.

>char * ret = (char*)malloc(sizeof(array) + 1 + 1);
sizeof doesn't do what you want here. It's giving you the size of a pointer, not the length of the string. If you want the length of the string, use strlen. And you really should be using new instead of malloc.
Last edited by Narue; Jun 12th, 2007 at 12:29 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: append char to char*

 
0
  #3
Jun 12th, 2007
  1. char * appendCharToCharArray(char * array, char a)
  2. {
  3. char * ret = new char(strlen(array) + 1 + 1); // + 1 char + 1 for null;
  4. strcpy(ret,array);
  5. ret[strlen(ret)] = a;
  6. ret[sizeof(ret)] = '\0';
  7. return ret;
  8. }

ok I have made the changes, used new char instead of malloc and used strlen instead of sizeof, but the same thing happens.

What sort of thing would I have to consider doing to get what I need.. maybe..

>convert char a to a string "a\0"
>use strcat() to concatenate strings
>return string value
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: append char to char*

 
0
  #4
Jun 12th, 2007
>but the same thing happens.
Okaaay, you clearly don't know what you're doing, so I think an example is in order:
  1. #include <cstring>
  2.  
  3. char *append_char ( const char *s, const char c )
  4. {
  5. std::size_t len = std::strlen ( s );
  6. char *ret = new char[len + 2];
  7.  
  8. std::strcpy ( ret, s );
  9. ret[len] = c;
  10. ret[len + 1] = '\0';
  11.  
  12. return ret;
  13. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: append char to char*

 
1
  #5
Jun 12th, 2007
A common solution to this problem is to pass around the array size along with the array. That way, it would work for all types of arrays, not just C style strings (char arrays with terminating null character).

  1. void doSomething(int* arr, int size)
  2. {
  3. //get ready to add some bugs to your code
  4. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: append char to char*

 
0
  #6
Jun 12th, 2007
>but I would prefer not to do this unless I have to?

Er why not?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC