-3

I want 2 strings to combine together.
First, I tried this one is OK.

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{
   char a[100] = "0";
   char b[100] = "1";
   //char *a = "0";
   //char *b = "1";

   printf("%s\n", a);
   printf("%s\n", b);
   strcat(a,b);
   printf("%s\n", a);

   return 0;
}

///////////////////////////
0
1
01

But, the second caused the problem. I can't figure out where not correct is.

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{
   //char a[100] = "0";
   //char b[100] = "1";
   char *a = "0";
   char *b = "1";

   printf("%s\n", a);
   printf("%s\n", b);
   strcat(a,b);
   printf("%s\n", a);

   return 0;
}
///////////////////////////
0
1
Segmentation fault

I have no idea why... Could somebody explain it ... please?

  • `char *a = "0";` sets `a` to point to a string literal, which is (conceptually) in read-only memory. Attempting to modify that area of memory causes undefined behavior - a segfault in your case. – Nate Eldredge Aug 09 '21 at 01:04
  • Strings are not really first-class objects in C. You have to think about them as arrays, and understand operations on them in terms of what is happening to the individual characters in that array. Understanding pointers is an important part of this. – Nate Eldredge Aug 09 '21 at 01:06
  • @kaylum: Note that your proposed duplicate is tagged C++, not C. At least one of the answers of that question contains information that does not apply to C. Therefore, I am not sure whether it is appropriate to mark it as a duplicate to a C question. However, apart from that point, the question does seem to be an exact duplicate. – Andreas Wenzel Aug 09 '21 at 01:25

2 Answers2

2

If you read the Linux Programmer's Manual about strcat(), it provides the function prototype in the form:

char *strcat(char *restrict dest, const char *restrict src) 

Then it explains what this means:

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.

The key here is "appends", which is to manipulate the string. However, In C, there are two ways to store strings:

Here is one way:

char *str = "Hello, World!";

And here is another way:

char str[] = "Hello, World!";

The difference here is that, the former stores the string "Hello, World!" in a read-only memory, and str is a pointer to that location; while, the latter stores the string in a writable memory.

In C, you are not allowed to manipulate what is stored in a read-only memory, and that is why your code does not work.

Jiho Kim
  • 158
  • 8
  • `char *a = "aaa"; printf("%s\n", a); a = "bbb"; printf("%s\n", a); ` But why can I manipulate the value here? I don't get what "read-only memory" means. – weichih25 Aug 09 '21 at 01:59
  • @weichih25: In that code, you are not changing the string literal itself. Instead, you are just making the pointer point to a different string literal, i.e. to a different memory location. – Andreas Wenzel Aug 09 '21 at 02:01
  • @weichih25 "Read-only memory" means that, you cannot change the content of whatever you stored there: it is like writing things in a permanent marker. When you compile the code you wrote on the comment, the C would first 1) Allocate a read-only memory for the string "aaa" then 2) store the address to that string in pointer variable a (like what Andreas Wenzel said). Note that it is "aaa" that is read-only, not the pointer variable a. That is why you can do things like a = "bbb" (but then the address to "aaa" would be lost forever). – Jiho Kim Aug 09 '21 at 02:10
  • @AndreasWenzel I see, thanks – weichih25 Aug 10 '21 at 07:19
0

The lines

char a[100] = "0";
char b[100] = "1";

allocate two arrays of length 100 each and initialize the arrays so that they contain the strings "0" and "1".

However, the lines

char *a = "0";
char *b = "1";

create two pointers that each point to a string literal, which is a string that resides in read-only memory.

Therefore, attempting to write into one of these strings using strcat causes undefined behavior (in your case a segmentation fault).

Even if the string literals were not located in read-only memory, your code would probably still not work, because when using strcat, you must also ensure that the destination string's memory buffer is large enough to contain both strings.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39