Preliminary understanding of c language: a simple description of pointers (emphasis)

Table of contents

Currently not talking about the complex usage of pointers

1. The role of the pointer

2. The concept of address and the principle of data storage

a address number

 Why does the address look like 0x00EFF858

pointer expression

3. The variable size of the pointer


Currently not talking about the complex usage of pointers

1. The role of the pointer

Multiple or different types of values ​​can be returned to make up for the shortcomings of arrays and functions.

In the process of programming, no matter whether it is storing data or taking data out, it needs to deal with the memory unit, and the computer expresses the memory unit through address coding. The pointer type is to store the address code and process the address data of the computer.

In addition to improving the efficiency of the program, the more important function of the pointer is to enable one function to access the local variables of another function , so the pointer is an indispensable tool for data exchange between the two functions.

2. The concept of address and the principle of data storage

first

We need to know that the memory in the computer will be divided into memory unit addresses

 

when we write the program

int main()
{    
int a = 10;

    return 0;
}

Among them, the variable a will apply for 4 bytes of space from the memory to store a (that is, 4 memory cells)


a address number

#include<stdio.h>
int main()
{    
int a = 10;
printf("%p\n",&a);//将其a的地址打印出来0x00eff858
    return 0;
}

 Why does the address look like 0x00EFF858

For example, int a=10; a applies for 4 bytes to the memory

0000 0000 0000 0000 0000 0000 0000 1010

Converted to hexadecimal is 0x0000000a


pointer expression

 #include<stdio.h>
int main()
{    
char ah = 10;
char*ah = &ah;//char 说明ah指向的对象是char类型  *说明p是指针变量
    return 0;
}

 

*p dereference operator

#include<stdio.h>

int main()
{    
char ah = 10;

char*p = &ah;//char 说明ah指向的对象是char类型  *说明p是指针变量

*p=20//解引用操作符,意思是通过p存放的地址,找到p所指代的对象。所以*p就是p指代的对象。
printf("%d\n",a);
//最后打印出的数字为20
    return 0;
}

3. The variable size of the pointer

Available operator sizeof to calculate (unit is byte)

 #include<stdio.h>

int main()
{    
     printf("%d\n",sizeof(char*));   
     printf("%d\n",sizeof(int*));//
     printf("%d\n",sizeof(float*));
     printf("%d\n",sizeof(double*));
     printf("%d\n",sizeof(short*));

    return 0;
}

In C/C++, the size of the pointer variable is fixed, and its size is related to the operating system and compiler. Specifically, the size of a pointer variable is usually 4 bytes or 8 bytes, depending on the bitness of the operating system and compiler.

On a 32-bit operating system, the size of a pointer variable is usually 4 bytes; on a 64-bit operating system, the size of a pointer variable is usually 8 bytes.

This is because the address bus width of the 32-bit operating system is 32 bits, which can address up to 2^32 (4 8bit or 4B) memory units, so a pointer variable needs 4 bytes to store the address. The 64-bit operating system has an address bus width of 64 bits, and can address up to 2^64 (8 8bit or 8B) memory units, so a pointer variable requires 8 bytes to store the address.

It should be noted that the size of a pointer variable is only related to the address space, but not to the type of object it points to. No matter what type of object the pointer variable points to, its size is fixed.

Refer to the maximum supported memory addressing space of 32, 64
 

Guess you like

Origin blog.csdn.net/fcccfffg/article/details/132039175