Struct MSG {
Char * P1;
Char * P2;
} Myptrs;
Myprts. p1 = "Teach Yourself C in 21 days ";
Myprts. P2 = "by SAMs publishing ";
I don't understand the assignment here. The address assigned to P1 is correct. How is it a string?
Also, why not 'Teach yourself C in 21 days ';
Instead
"Teach Yourself C in 21 days ";
When are single quotes and double quotes used?
*************************************
Reply to: mystaring)
Just like char P1 [5] = "ABCD", P1 is only an address pointing to the first address of the string and ends when '/0' is output.
Printf ("% s", P1); will output ABCD;
However, if it is defined as char P1 [4] = "ABCD" at the beginning, the output string will not only be ABCD, but also garbled characters later, because the machine will always end with '/0' in memory.
*************************************
Reply to: Foochow (just like your gentleness)
Single quotation marks are used to give a character constant, while double quotation marks are used to give a String constant. The former (a character constant) is used by the compiler to directly calculate the corresponding value and assign it to a variable or participate in the expression operation, it does not occupy space in the memory, while the latter (String constant) will allocate a certain unit in the memory to put the string, and pass the starting address to a pointer or as a function parameter.
*************************************
Reply to: kgdiwss (go to dinner tomorrow)
In other words:
Myprts. p1 = "Teach Yourself C in 21 days ";
Myprts. P2 = "by SAMs publishing ";
The first address of the string "Teach Yourself C in 21 days" and "by SAMs publishing" is assigned to P1 and P2, right. If * P1 and * P2 are read, the read starts from the first address until/0 is read. Is that true?
However, printf ("% s", P1); does this P1 Save the address? How can I output the address to % s. Why not:
Printf ("% s", * P1 );
*************************************
Reply to: Foochow (just like your gentleness)
However, printf ("% s", P1); does this P1 Save the address? How can I output the address to % s. Why not:
Printf ("% s", * P1 );
.................
This is a special character of the string pointer. When the pointer is output, printf ("% s", P1) will output the character of the string pointed to by P1 until '/0, printf ("% s", * P1); only one character is output.
*************************************
Thank you for your reply.