[C Language Elementary] Pointer - Part 1


insert image description here

1. What is a pointer?

What are pointers? Two key points for pointer understanding:
> 1. 指针是内存中一个最小单元的编号,也就是地址
> 2. 平时口语中说的指针,通常指的是指针变量,是用来存放内存地址的变量

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


Then we can understand the above two points in this way:

① Memory

insert image description here
Supplementary knowledge

①Each memory unit has one and only one address number, which is also called address

number == address == pointer

②When writing a C language program, the created variables, arrays, etc. must open up space in the memory.


② 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指针变量

#include <stdio.h>
int main()
{
    
    
 int a = 10;//在内存中开辟一块空间
 int *p = &a;//这里我们对变量a,取出它的地址,可以使用&操作符。
    //a变量占用4个字节的空间,这里是将a的4个字节的第一个字节的地址存放在p变量
     //中,p就是一个之指针变量。
 return 0;
}


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

  • How big is a small unit - answer: (one byte)
  • How to 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) when addressing is (1 or 0);

Then the address generated by the 32 address lines will be:

00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001

11111111 11111111 11111111 11111111

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 32/1024/1024/1024GB == 4GB) 4G space for addressing.
In the same way, for a 64-bit machine, if 64 address lines are given, how much space can be addressed can be calculated by yourself.


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.

Summarize:

  • Pointer variables are used to store addresses, which 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

Here we are discussing: we all know the types of pointers, and variables have different types, integer, floating point, etc.
那指针有没有类型呢?
To be precise: yes.
When there is code like this:

int num = 10;
p = &num;

要将&num(num的地址)保存到p中,我们知道p就是一个指针变量,那它的类型是怎样的呢?
We give the corresponding type to the pointer variable.

char  *pc = NULL;
int   *pi = NULL;
short *ps = NULL;
long  *pl = NULL;
float *pf = NULL;
double *pd = NULL;

As you can see here, the definition of the pointer is: type + * .
Actually:
The pointer of char* type is to store the address of char type variable.
The short* type pointer is used to store the address of the short type variable.
The pointer of type int* is used to store the address of variable of type int.
那指针类型的意义是什么?


2.1 pointer ± integer

#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;
}

Summarize:指针的类型决定了指针向前或者向后走一步有多大(距离)


2.2 Dereferencing of pointers

//演示实例
#include <stdio.h>
int main()
{
    
    
 int n = 0x11223344;
 char *pc = (char *)&n;
 int *pi = &n;
 *pc = 0;   //重点在调试的过程中观察内存的变化。
 *pi = 0;   //重点在调试的过程中观察内存的变化。
 return 0;
}

Summarize:

指针的类型决定了,对指针解引用的时候有多大的权限(能操作几个字节)。


For example: dereferencing a char* pointer can only access one byte, while dereferencing an int* pointer can access four bytes.



3. Wild Pointer

3.1 Causes of wild pointers

1. Uninitialized pointer

#include <stdio.h>
int main()
{
    
     
 int *p;//局部变量指针未初始化,默认为随机值
    *p = 20;
 return 0;
}

2. Pointer out-of-bounds access

#include <stdio.h>
int main()
{
    
    
    int arr[10] = {
    
    0};
    int *p = arr;
    int i = 0;
    for(i=0; i<=11; i++)
   {
    
    
        //当指针指向的范围超出数组arr的范围时,p就是野指针
        *(p++) = i;
   }
    return 0;
}

3. Release the space pointed to by the pointer

这里放在动态内存开辟的时候讲解,这里可以简单提示一下.



3.2 How to avoid wild pointers

  1. pointer initialization
  2. Careful pointer out of bounds
  3. The pointer points to the space to release, and set NULL in time
  4. Avoid returning the address of a local variable
  5. Check the validity of the pointer before using it
#include <stdio.h>
int main()
{
    
    
    int *p = NULL;
    //....
    int a = 10;
    p = &a;
    if(p != NULL)
   {
    
    
        *p = 20;
   }
    return 0;
}

指针初阶内容下期更新完哦!!!

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_58286439/article/details/131954887