Today, in the reading of a piece of code, I found that there is a code problem, the code for the error of the explanation.
and reinforce some of the differences between the two methods of defining strings that we commonly use to explain.
The error code on the book is probably as follows:
#include <stdio.h>voidCopyChar*x,Char*y) { for(; *y!=' /'; x++,y++) *x=*y; *x=' /';
printf ("%s\n", a);}intMain () {Char*a="hello!"; Char*b="world!"; Copy (A, b); return 0;}
Both character arrays and string pointers can be used to store strings.
But the character array and the string have the essential difference.
#include <stdio.h>intMain () {Chara[Ten]="hello!"; Char*b="world!"; printf ("a=%x\n", a);printf ("&a[0]=%x\n", &a[0]); printf ("b=%x\n", B); printf ("&b=%x\n",&b);}
A=9ffe40&a[0]=9ffe40b=404000&b=9ffe38
With the output we can see that the character array A is an address that is the first address of this array of characters &a and &a[0] is the first address of the character array.
And the string pointer pointer variable B is the first address used to store a string of characters &b is the address of the data stored in this pointer variable.
We can make a single character change to the character array, because the character array is a bunch of characters, so we modify one of the elements.
But a string pointer is a pointer variable of a character type to a string, the string is stored in the static data, for these already defined constants we can not be arbitrarily modified.
Char *a="hello! " ; Char *a;a="hello! ";
The two definitions have the same effect, and if we look at them separately, we'll see them more clearly.
The string that the string pointer points to cannot be replaced with another string.
So when it comes to this, we understand the wrong location and the cause of the program in the book.
*a=*B; *a='+ ';
Both statements are error string pointers that cannot be modified in this manner. This is obviously a bad code.
In order to achieve this effect I can have a lot of writing here to give a few examples:
#include <stdio.h>void copy (char *a,char *b) { a=b; printf ("%s\n", a);} int Main () { char *x="hello! " ; Char *y="world! " ; Copy (x, y); }
This example is to give the address of B directly to a so that a, sister, A and a, the same string, also play the role of assignment.
#include <stdio.h>voidCopyCharA[],Charb[]) { intI=0; for(i=0;i<sizeof(b); i++) {A[i]=B[i]; } printf ("%s\n", a); } intMain () {Charx[]="hello!"; Chary[]="world!"; Copy (x, y); }
This method is defined as the definition of the character array so that you can achieve a character of a character assignment.
So the story is not necessarily all right, programming this kind of thing never the only correct answer, the realization of a function of different people who have different code implementation methods are not the same.
So move hands, for every code are skeptical attitude, no absolute authority, to explore more new things to their own so as to have greater progress, always tread the footsteps of others can never exceed others.
Definition of character arrays and string pointers