CS1102C Miniature 1

What is the difference between assignment and strcpy() when using C strings?

The key to understanding C strings and their related functions is to be able to visualize where the character pointers go to, and whether a string buffer is required.

First, I would like to mention that in C and C++ notation, character array identifiers and character pointers are often interchangeable, so the following are legal syntax:

char* s1 = "abc";
char s2[5] = "def";
char c = s1[1];  // legal
char* s = s2;    // legal

However, note that the assignment of a character pointer to an array identifier is illegal in both C and C++, and will cause compilation errors:

char* s1 = "abc";
char s2[5];
s2 = s1;  // illegal

Also, note that string literals (e.g. "abc") are stored in some protected area of the memory and cannot be overwritten. Hence, the following is valid syntax, but causes segmentation fault:

char* s = "abc";
s[1] = 'd';  // segmentation fault

Now consider the simple C string assignment:

char* s1 = "abc";
char* s2;
s2 = s1;

We can visualize it using the before and after diagrams below:

Before the assignment

Before the assignment, s1 points to some pre-allocated string buffer and s2 points to some undefined memory location.

After the assignment

After the assignment, both s1 and s2 points to the same location.

Next, we consider the strcpy() function:

char* s1 = "abc";
char s2[5];
strcpy(s2, s1);

Again, we can visualize it using the before and after diagrams below:

Before the strcpy() function

Before calling the strcpy() function, s2 is an uninitialized character array.

After the strcpy() function

After calling the strcpy() function, the contents of the s1 array is copied into the s2 array. However, s1 and s2 point to different memory locations.

Knowing how strcpy() works, we can generalize the idea to almost all C string functions, including strcat() and sprintf(). The first parameter of these functions expect a character pointer to an existing buffer, which the function will write into. Hence, the following is very likely to produce a segmentation fault:

char* s;
sprintf(s, "%04d", 8);  // segmentation fault

I shall end this miniature with an exercise:

char s1[] = "xyz";
char* s2;
char s3[256];
s2 = s1;
strcpy(s3, s2);
s1[0] = s2[1];
strcat(s2, s3);
s2 = &s3[2];
s2[0] = s3[0];
printf("%s %s %s\n", s1, s2, s3);

What is the output of the above segment of code?

Back to Index