Pointer, pointer variable, and memory space -- Unlock

Source: Internet
Author: User

Pointer, pointer variable, and memory space -- Unlock

 

 

 

A year ago, I wrote a blog about defining a pointer and pointer variable on chinaunix and then moved to csdn. This blog wrote many common errors about pointers, pointer variables, and space allocation. However, until now, no matter csdn users, working colleagues, or junior or senior students, someone has been asking me about pointer and pointer variables and space allocation issues. I want to write another blog here to help you solve the problem.

 

 

Question 1

Int idata = 5; <br/> int * P = idata // Why is this incorrect?

Since * P usually indicates a reference to the pointer P pointing to data, why * P = idata cannot be used?

 

A: int * P is different from the reference * P. When defining data, int * P indicates a pointer P pointing to int data. It is not the * P.

Therefore, int * P only defines a pointer P pointing to int data, which stores an address (32 bits) and idata is an int data, int data cannot be assigned to an address. Except for definitions, * P indicates the data pointed to by P.

So the above statement is correct:

Int idata = 5; <br/> int * P = & idata; // P address for idata Storage

 

 

 

Question 2:

As mentioned in question 1, the definition only defines a pointer P. Why is the following statement correct?

Char * P = "Hello World"; // Why is it correct?

 

Answer:

In C, character string constants are processed by string arrays, and a string array is opened in the memory to store the string constants, then, assign the first element address of the string number to P.

The preceding statements are equivalent to the following:

Char * P = "Hello World"; <br/> // equivalent to <br/> char TMP [] = "Hello World "; <br/> char * P = TMP; <br/> // equivalent to <br/> char TMP [] = "Hello World "; <br/> char * P = NULL; // initialization to avoid compilation errors <br/> P = TMP;

 

 

Question 3:

For the struct pointer, struct mystruct * pstruct and then operate on pstruct. Why is sometimes correct and sometimes memory error reported?

 

 

Answer:

Pointer operations include input and output. Input: refers to the pointer pstruct pointing to the struct to be passed in for processing. Output: other functions need to be output and must be written.

What you call is sometimes correct and sometimes incorrect, because you have not figured out who should allocate space. Be sure to keep in mind that defining a pointer only defines a pointer variable that stores the address. It can only store the address, and defining a pointer does not allocate space for the data to which it points. For example, struct mystruct * P defines a pointer Variable P pointing to struct mystruct without allocating the size of struct mystruct. If P does not point to any data, you must write it, and a memory error will occur.

However, if you do this:

 

Struct mystruct myst;

Struct mystruct * P = & myst;

 

Then, P is operated, and no error occurs. After all, I still haven't figured out who needs to allocate space for data in programming.

 

 

Recently, in the book C ++ primer, there is a very classic sentence:

When attempting to understand pointer declarations, read them
From right to left.

When you understand the pointer definition, read from right to left.

 

 

 

 

 

Copyright statement:
Reprinted Article please indicate the original source http://blog.csdn.net/feiyinzilgd/archive/2011/01/05/6119090.aspx

Contact tan Haiyan or go to tan Haiyan's personal homepage to leave a message.

 

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.