| | |
append char to char*
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
Hi I would like to create a function that appends a character to a character array.. this is what I have got so far..
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.
c Syntax (Toggle Plain Text)
char * appendCharToCharArray(char * array, char a) { char * ret = (char*)malloc(sizeof(array) + 1 + 1); strcpy(ret,array); ret[strlen(ret)] = a; ret[sizeof(ret)] = '\0'; return ret; }
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.
>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.
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.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
c Syntax (Toggle Plain Text)
char * appendCharToCharArray(char * array, char a) { char * ret = new char(strlen(array) + 1 + 1); // + 1 char + 1 for null; strcpy(ret,array); ret[strlen(ret)] = a; ret[sizeof(ret)] = '\0'; return ret; }
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
>but the same thing happens.
Okaaay, you clearly don't know what you're doing, so I think an example is in order:
Okaaay, you clearly don't know what you're doing, so I think an example is in order:
C++ Syntax (Toggle Plain Text)
#include <cstring> char *append_char ( const char *s, const char c ) { std::size_t len = std::strlen ( s ); char *ret = new char[len + 2]; std::strcpy ( ret, s ); ret[len] = c; ret[len + 1] = '\0'; return ret; }
I'm here to prove you wrong.
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).
c Syntax (Toggle Plain Text)
void doSomething(int* arr, int size) { //get ready to add some bugs to your code }
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- I/O question. using vc++ read char by char (C++)
- Problems casting a const char* to char* (C++)
- OOP char [8] to char (C++)
Other Threads in the C++ Forum
- Previous Thread: processing/modifying a string
- Next Thread: Lotus NOTES and C++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






