C adding char to char *

So I'm trying to add char to char* .

For example, I have char *word = " "; I also have char ch = 'x';

I am doing append(word, ch); Using this method.

 void append(char* s, char c) { int len = strlen(s); s[len] = c; s[len+1] = '\0'; } 

This gives me a segmentation error, and I understand why I suppose. Because s[len] goes beyond. How to make it work? I need to also clear char* if I used something like the word char [500]; How can I understand this if he has some characters attached to him? Will strlen this always be 500 ? Thanks in advance.

+7
source share
5 answers

A typical C practice would be this:

 //returns 1 if failed, 0 if succeeded int append(char*s, size_t size, char c) { if(strlen(s) + 1 >= size) { return 1; } int len = strlen(s); s[len] = c; s[len+1] = '\0'; return 0; } 

When passing a function, the array for changing the function has no idea at compile time how much space it has. A common practice in C is also to pass the length of the array, and the function will trust this binding and fail if it cannot complete its work in the space that it has. Another option is to redistribute and return a new array, you will need to return char* or take char** as input, but you should carefully think about how to manage heap memory in this situation. But without redistribution, yes, your function must somehow fail if it is asked to add when there is no free space, for you, as a failure.

+10
source

It's hard to add a line to a place in C. Try something like this:

 char *append(const char *s, char c) { int len = strlen(s); char buf[len+2]; strcpy(buf, s); buf[len] = c; buf[len + 1] = 0; return strdup(buf); } 

Be sure to free the returned string when you're done with it.

FYI: This is probably due to the fact that the line you are passing is stored in read-only memory. But you are right, you also write off from the end ( [len+1] write, not [len] ).

+5
source

If you pass in

 append("foo", 'X'); 

it will work because foo is usually placed in read-only storage. Even if it is not, it can rewrite something bad! In this case, the compiler, if good, should warn you about converting from const char * to char *, which would be the key.

+3
source

Yes, the assumption you made is almost - correct - the accident may be due to the fact that you are trying to write outside the line boundaries (in fact, only s[strlen(s) + 1] is beyond the scope because s[strlen(s)] is still a valid location - the final NUL byte is stored there). But you also cannot change the string literal because it is usually found in some part of the read-only process. Both of these actions result in undefined behavior that may fail. You can solve this problem by copying the string to dynamically allocated storage and then modifying the copy. In addition, you should use const char * in the argument of your function, because char * assumes that read-only strings cannot be passed.

 char *append(const char *orig, char c) { size_t sz = strlen(orig); char *str = malloc(sz + 2); strcpy(str, orig); str[sz] = c; str[sz + 1] = '\0'; return str; } 

Also, don't forget free() to return a string when it is no longer needed.

0
source

You cannot safely add to an arbitrary string, because, firstly, string constants tend to be in constant memory, so trying to write them can lead to a segmentation error, and secondly, you don’t have to guarantee that if they passed you a buffer that you did not shoot at the end of it.

In particular, if you do this, char x [500];

there is no guarantee that strlen (x) will return 500 to you. It will return to you how many characters it needs to count from the beginning of x before it reaches zero. It can return you 0, 1 ... 500, 501 ..., depending on what is in x.

Indeed, your only options are to call append with the size of the buffer you add (so that you can do something suitable if the buffer is full) or make append allocate a new buffer each time it is called, in which case, of course, you need to free the buffer.

0
source

All Articles