2021.1.17-Preliminary understanding of pointers and structures

Preliminary understanding of pointers

int main()
{
        int a = 0;
        int* p= &a;
        printf("%d",*p);
        return 0;
}

Initial contact with the pointer, it feels not as horrible as I heard before, maybe it's just that I have just touched the skin, and I will feel it when I touch the deeper things in the future. Please work harder.
First of all, we need to know what is meant by the above code, int a =0 is obviously the process of assigning "0" to the integer variable a; we have to understand that p is a pointer variable, which is used to store addresses A variable.
p is to find the content of the address pointed to by
p. The p variable to store the address of a (&a)
tells us that p is a pointer variable, and int tells us that the content of the address pointed to by p is an integer type
pointer variable. The address length is not based on what it points to It is determined by the type of content, but by the compilation environment. 32-bit is 4 bytes, and 64-bit environment is 8 bytes;

The preliminary understanding of the structure
We all know that code is a way to construct the real world, but the entity in the real world is not a single attribute, every entity is a complex object. For example, when we want to describe a person, we must have the information of name, height, age, and ID number. At this time, we can’t simply use arrays or definitions to achieve this, we must use a structure to create a Type it out.
Instructions:

struct Book //创造一个结构体类型
{
   char name[20];
   short price;
}

In this way, a Book type is simply constructed, and when used in a function, variables must be defined before it can be used. struct Book book1 = {"Preliminary understanding of C language", 55}; In
this way, we have explained that a book is "C language" A preliminary understanding" and the price of 55 yuan book is defined.
So what if we just want to know the title of the book?
The first method
will use our operator "." at this time, it can access the members of our book1, the name member: book.name; the price member: book.price; the
second method
is created for book1 Pointer, and then use the operator "->" to complete, use: structure pointer variable -> member name. Note: The pointer type used to create book1 is struct Book*

How to modify the structure When
modifying the integer variable in the structure member, you can directly assign the value to overwrite the previous value,
int book1.price = 36; that's it,
and when you modify the string type in the structure member, you must use it The function strcpy() can be used only when the header file string.h is introduced.
How to use: strcpy(book1.name,"C++");

Well, I learned this little knowledge on January 17, 2021, but I think it is enough for a novice like me to understand!

Guess you like

Origin blog.51cto.com/15083094/2593907