简体   繁体   中英

Error in appending two string pointers using strncat()

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 ‘appendOneChar’:
temp.c:7:17: warning: passing argument 1 of ‘strncat’ makes pointer from integer without a cast [-Wint-conversion]
    7 |  return strncat(*first_str, *N, 1);
      |                 ^~~~~~~~~~
      |                 |
      |                 char
In file included from temp.c:3:
/usr/include/string.h:133:40: note: expected ‘char * restrict’ but argument is of type ‘char’
  133 | extern char *strncat (char *__restrict __dest, const char *__restrict __src,
      |                       ~~~~~~~~~~~~~~~~~^~~~~~
temp.c:7:29: warning: passing argument 2 of ‘strncat’ makes pointer from integer without a cast [-Wint-conversion]
    7 |  return strncat(*first_str, *N, 1);
      |                             ^~
      |                             |
      |                             char
In file included from temp.c:3:
/usr/include/string.h:133:71: note: expected ‘const char * restrict’ but argument is of type ‘char’
  133 | extern char *strncat (char *__restrict __dest, const char *__restrict __src,
      |                                                ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
Segmentation fault

So what's going on here?

Expected output: firstsecond

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM