It is difficult to assign values to the char pointer in the structure.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.