943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 45732
  • C++ RSS
Jun 12th, 2007
0

append char to char*

Expand Post »
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.
Similar Threads
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 12th, 2007
0

Re: append char to char*

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 12th, 2007
0

Re: append char to char*

  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
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 12th, 2007
0

Re: append char to char*

>but the same thing happens.
Okaaay, you clearly don't know what you're doing, so I think an example is in order:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 12th, 2007
1

Re: append char to char*

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. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Jun 12th, 2007
0

Re: append char to char*

>but I would prefer not to do this unless I have to?

Er why not?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: processing/modifying a string
Next Thread in C++ Forum Timeline: Lotus NOTES and C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC