I've a function that is supposed to return a string with char *first_str
& char *N
appended
char *append(char *N) {
char *first_str = "first";
return strcat(*first_str, *N);
}
int main(void) {
char *N = "second";
printf("%s", append(N));
return 0;
}
It gives the following warning messages and segfaults at the end:
temp.c: In function ‘append’:
temp.c:7:16: warning: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
7 | return strcat(*first_str, *N);
| ^~~~~~~~~~
| |
| char
In file included from temp.c:3:
/usr/include/string.h:130:39: note: expected ‘char * restrict’ but argument is of type ‘char’
130 | extern char *strcat (char *__restrict __dest, const char *__restrict __src)
| ~~~~~~~~~~~~~~~~~^~~~~~
temp.c:7:28: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
7 | return strcat(*first_str, *N);
| ^~
| |
| char
In file included from temp.c:3:
/usr/include/string.h:130:70: note: expected ‘const char * restrict’ but argument is of type ‘char’
130 | extern char *strcat (char *__restrict __dest, const char *__restrict __src)
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
Segmentation fault
So what's going on here?
Expected output:
firstsecond
CodePudding user response:
To demonstrate, a working code would look like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *append(char *N) {
char *first_str = malloc(strlen(N) 6); // 6 = strlen("first") 1 (null terminator)
if (first_str== NULL) {
// malloc failed, handle error
return NULL;
}
strcpy(first_str, "first");
return strcat(first_str, N);
}
int main(void) {
char *N = "second";
char *concat = append(N);
if (concat == NULL) {
// append function returned an error
return 1;
}
printf("%s", concat);
free(concat); // Free the dynamically allocated memory
return 0;
}
From the strcat manual: char *strcat(char *destination, const char *source)
Note: When we use strcat(), the size of the destination string should be large enough to store the resultant string. If not, we will get the segmentation fault error. That's why you need an array of size 12 (5 6 1), notice the 1 for the '\0' that is stored in the end of the concatenated strings.
Note 2: You need malloc to allocate memory to the heap in order for the memory to remain until you use "free" to clear it. The variables you create within a function will be allocated on the stack and will be automatically freed when the function returns. So in your example when the function "append" returns the memory is no longer usable.