Getting to know pointers (C language)

1. The concept of pointers (introduction)

1. What is a pointer

  1. A pointer is the number of the smallest unit in memory, that is, the address
  2. The pointer in the spoken language usually refers to the pointer variable, which is a variable used to store the memory address

A pointer is an address, and a pointer in spoken language usually refers to a pointer variable.

Then it can be understood in this way
1. Memory
insert image description here
pointer variable

We can use & (address operator) to take out the memory start address of the variable, and store the address in a variable, which is a pointer variable

Summary: Pointer variables, variables used to store addresses. (The value stored in the pointer is treated as an address).

2. Memory (Introduction)

For a 32-bit machine, assuming that there are 32 address lines, then assuming that each address line generates a high level (high voltage) and a low level (low voltage) when addressing is (1 or 0);

Then the address generated by the 32 address lines will be:
insert image description here

There are 2 to 32 addresses here.
Each address identifies a byte, then we can give (2^32Byte == 2^32/1024KB == 2^32/1024/1024MB == 2^/1024/1024/1024GB == 4GB) 4G space for addressing.
In the same way, if the 64-bit machine is given 64 address lines, it can address 8G space.

On a 32-bit machine, the address is a binary sequence composed of 32 0s or 1s, and the address must be stored in 4 bytes, so the size of a pointer variable should be 4 bytes.
Then if on a 64-bit machine, if there are 64 address lines, the size of a pointer variable is 8 bytes to store an address.

Summary:
Pointer variables are used to store addresses, and addresses uniquely identify a memory unit.
The pointer size is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms.

2. Pointer type

Variables have different types, integer, floating point, etc. Does the pointer have a type?

int main()
{
    
    
	int* p1 = NULL;//NULL是空指针
	char* p2 = NULL;
	float* p3 = NULL;
	return 0;
}

From the above code we can know: the definition of the pointer is type + *

For example:
a pointer of type int is used to store the address of a variable of type int
A pointer of type char is used to store
the address of a variable of type char
A pointer of type float is used to store the address of a variable of type float*

3. Storage of pointers

Variables are created in memory (space is allocated in memory), and each memory unit has an address, so variables also have addresses. Take out the variable address as follows:

#include <stdio.h>
int main()
{
    
    
 int num = 10;
 &num;//取出num的地址
    //注:这里num的4个字节,每个字节都有地址,取出的是第一个字节的地址(较小的地址)
 printf("%p\n", &num);//打印地址,%p是以地址的形式打印
 return 0;
}

insert image description here
How to store the address requires defining a pointer variable.

int num = 10;
int *p;//p为一个整形指针变量
p = &num;

Examples of using pointers:

#include <stdio.h>
int main()
{
    
    
 int num = 10;
 int *p = &num;
 *p = 20;
    return 0;
}

Fourth, the size of the pointer variable

#include <stdio.h>
//指针变量的大小取决于地址的大小
//32位平台下地址是32个bit位(即4个字节)
//64位平台下地址是64个bit位(即8个字节)
int main()
{
    
    
    printf("%d\n", sizeof(char *));
    printf("%d\n", sizeof(short *));
    printf("%d\n", sizeof(int *));
    printf("%d\n", sizeof(double *));
    return 0;
}

Conclusion: The pointer size is 4 bytes on a 32-bit platform and 8 bytes on a 64-bit platform.

insert image description here

Guess you like

Origin blog.csdn.net/ikun10001/article/details/130159610