[C Language Elementary] Take you to easily master the basics of pointers (1) - the definition, type, and size of pointers

insert image description here

Junxi_'s personal homepage

Be diligent and encourage the years to wait for no one

C/C++ game development


Hello, this is Junxi_, I’m a bit lazy when I just got home recently, and I’m going to resume updating today and start updating a new series of brushing questions. Let’s continue to update the content of the 0-basic entry-level C language. Today I’m going to bring you pointers. But since it’s an elementary level, we’ll put some advanced usage in the advanced chapter, and today we’ll just talk about the basics.

1. What is a pointer?

  • For beginners, the most confusing thing is the meaning of pointers, which leads to wrong applications, so let's talk about what pointers are today.

Two key points for understanding pointers:
1. A pointer is the number of the smallest unit in memory, that is, the address
2. The pointer we usually refer to in spoken language usually refers to a pointer variable, which is a variable used to store memory addresses

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

Memory and Pointer Variables

  • 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 our pointer variable

#include <stdio.h>
int main()
{
    
    
    int a = 10;//在内存中开辟一块空间
    int* p = &a;//这里我们对变量a,取出它的地址,可以使用&操作符。
    //a变量占用4个字节的空间,这里是将a的4个字节的第一个字节的地址存放在p变量中,p就是一个之指针变量。
    return 0;
}
  • Summarize:
  • A pointer variable is a variable used to store an address. (The value stored in the pointer is treated as an address).
  • The question here is:
  • How big is a small unit? (1 byte)
  • And how to address?
  • After careful calculation and weighing, we found that it is more appropriate to assign a byte to a corresponding address.
  • 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) during addressing (1 or 0); then the address generated by the 32 address lines will be
    :
    insert image description here
  • 64位机器同理,有64位地址线,这里仅以32位机器为例。
  • 二进制,由于有32根地址线,所以一共有2的32次方个字节的空间,也就产生了这么多的编号。
    insert image description here
  • We know (2^32Byte = 2^32/1024KB =2^32/1024/1024MB = 2^32/1024/1024/1024GB = 4GB)
  • At this point we can address the 4GB space.
  • Here we understand:
  • 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. Pointers and pointer types

  • Like variables, pointers also come in different types.
int *p;     //定义一个指针变量P,指向整形变量 
char *p;		// 定义一个指针变量p,指向字符型变量 
float * p; 		// 定义一个指针变量p,指向单精度浮点型变量 
double *p1, *p2;		//定义两个指针变量p1和p2,指向双精度实型变量 
  • 这里可以看到,指针的定义方式是: type + * 。(type指类型名)

pointer ± integer

  • Let's take a look at the code:
#include <stdio.h>
//演示实例
int main()
{
    
    
    int n = 10;
    char* pc = (char*)&n;
    int* pi = &n;
    printf("%p\n", &n);
    printf("%p\n", pc);
    printf("%p\n", pc + 1);
    printf("%p\n", pi);
    printf("%p\n", pi + 1);
    return 0;
}

insert image description here

  • The %p here means to print the address.
  • 1. From the printed results, we can see that the address of &n and pc are the same, that is to say, they both point to the same place. (Here, since n is an int type, we need to cast it to a char type before using it)
  • 2. From 2 and 3, we can see that adding 1 to the pointer means adding 1 to the address, that is, the address points to the next bit.
    - 3. From 3, 4, and 5, we can see that the addresses of different types of pointers plus 1 are different, which are related to the type of pointer variable.
  • Summary: The type of pointer determines how far the pointer takes one step forward or backward (distance)
  • However, here we need to know that under the same operating system, the size of the pointer variable is the same!

the size of the pointer variable

  • Above we talked about the way pointers are stored in memory and the size of the storage space. As much space as there is, there will naturally be as many addresses, that is, as many pointers.
  • Test it with code:
#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;
}

insert image description here

  • My computer is a 64-bit platform, so the size of the pointer is 8 bytes

Summary:
The address under the 32-bit platform is 32 bits (that is, 4 bytes)
The address under the 64-bit platform is 64 bits (that is, 8 bytes)

pointer dereferencing

#include <stdio.h>
int main()
{
    
    
    int n = 0x11223344;
    
    int* pi = &n;
    *pi = 0;
   char* pc = (char*)&n;
   *pc = 0;
    return 0;
}

insert image description here

  • The above is the storage of pi in memory, when we change the value in pi to 0
    insert image description here
  • The data stored in the 4 bytes are all changed to 0
    insert image description here
  • The above is the address of the pc in the memory. We can see that when the data in the address is also changed to 0, only the address of one byte is changed.
  • Summarize:
  • The type of the pointer determines how much authority (how many bytes can be manipulated) when dereferencing the pointer.
  • For example: the dereference of the pointer of char * can only access one byte, and the dereference of the pointer of int * can access four bytes.

Summarize

  • Today's content is over here for the time being. Today, we will first talk about the definition, type, size, and differences of different types of pointers. We will explain the rest in batches

  • Well, if you have any questions, please ask me in the comment area or private message, see you next time!

It is not easy for a new blogger to create. If you feel that the content of the article is helpful to you, you may wish to click on this new blogger before leaving. Your support is my motivation to update! ! !

**(Ke Li asks you to support the blogger three times in a row!!! Click the comment below to like and collect to help Ke Li)**

insert image description here

Guess you like

Origin blog.csdn.net/syf666250/article/details/131497865