Define a pointer and a pointer variable

Source: Internet
Author: User

Struct stat st;
Struct stat * st;

 

 

 

[Case-based explanation]
Struct stat * st;
Is a pointer that defines a stat struct,
This sentence focuses on "pointer", which is essentially a 32-bit number (in a 32-bit architecture.
For names and entities:
Name:
A pointer named St
Entity:
In the memory, there is only one 32-bit space to store this pointer.

Struct stat st;
Defines a struct of the stat type, which is named St.
The key here is the "struct", which occupies different physical space sizes according to the specific definition of the struct.
The name and entity here:
Name, which is a struct called St.
Entity:
It is a struct. The size of the struct occupies a large amount of physical space.
That is to say, for example, if your struct is very complex, for example, if all the member spaces add up to 80 bytes, then the st actually occupies 80 bytes.

[Why are mistakes easy]
After explaining the meaning, Let's explain why such a mistake was made,
We have defined this:
Struct stat * st;
However, it is actually just a pointer to define the required struct, rather than the actual struct itself.

For example, a struct is like a room.
Different types of rooms have different sizes and occupy different physical spaces.

Struct pointer is like a key in a room.
Regardless of the size of the relevant room, the key always occupies a space as large as the key (by default, it can be understood as 4 bytes, 32 bits ).

The reason we make such a mistake is, in the final analysis, it is too greedy. When we get a pointer (key), the system will automatically allocate it to you,
What the Pointer Points to (someone else will give you a house ...).

[Extension]
Whether you see similar pointers elsewhere,
Whether the key can be opened, a small room, a villa, a car, or a toy car,
It's just a key, it's just that big. The physical allocated space is also 32 bits.

Remember, if you don't get another house or car, you just get the key. Unless you buy a house (apply for a structure), no one will send it to you.

Sub-structure (the system does not care where your pointer points, or assign another struct to you ).

If you haven't fully understood it, you just need to remember the simplest method:
Key = 32-bit = 4 bytes

In this way, you will not be confused.

[Postscript]
If you only get the key and have a house and a car, now there will not be so much depressing about buying a house and buying a car,
Just copy it for a few yuan...
 
 
 
Personal Understanding:
Similarly, this is true for struct, and the same is true for defining other types.
Char * str1;
Char str2 [20];
After this definition, we only pass the str1 pointer, instead of passing a string to str1.
Use the following command after definition:
 
Sprintf (str1 ,"....",..);
So, this is equivalent to installing something in str1, but str1 is an address in the memory (it can be seen as a number ), therefore, str1 occupies only one or two bytes in the memory (based on OS ). When a string is loaded into str1, the segment fault segment error occurs.
To do this, you need to use malloc to apply for space for SR1.
 
However, this is legal:
Char * str1 = "asdfg"; Define and initialize, the system will allocate a continuous space for str1 to put this string, the first address of the string is placed in str1.
 
 
Str1 [20] in the memory, whether you use it or not, it will have 20 bytes.

 

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.