[C Language Elementary] Take you to easily master the basics 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 Jun Xi_, today we will continue to update the content of the 0-basic entry-level C language. Our main update this time is still the basic knowledge of elementary pointers

  • Without further ado, let's start directly! !

1. Wild pointer

Concept: A wild pointer means that the location pointed to by the pointer is unknown (random, incorrect, and without clear restrictions)

1. Causes of wild pointers

    1. pointer is not initialized
#include <stdio.h>
int main()
{
    
    
int *p;//局部变量指针未初始化,默认为随机值
  *p = 20;
return 0;
}
    1. 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;
  }
    1. The space pointed to by the pointer is freed
  • Here it is explained when dynamic memory is opened up in the pointer advanced chapter

2. How to avoid wild pointers

1. Pointer initialization
2. Be careful that
the pointer is out of bounds 3. Release the space pointed to by the pointer and set NULL in time
4. Avoid returning the address of the 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;
  }

2. Pointer arithmetic

1. pointer ± integer

#define m 5
float values[m];
float* vp;
//指针+-整数;指针的关系运算
for (vp = &values[0]; vp < &values[m];)
{
    
    
    *vp++ = 0;//把values数组中每个元素都初始化为0,这里指针++表示地址后移
}

2. Pointer-Pointer

//计算字符串的长度
int my_strlen(char *s)
{
    
    
   char *p = s;
   while(*p != '\0' )
       p++;
   return p-s;//当p中读取到\0时说明该字符串总长度为此时的地址p-首元素地址s
   }

3. Relational operations on pointers

for(vp = &values[m]; vp > &values[0];)
{
    
    
  *--vp = 0;//由于是前置--,最后会与第一元素之前的元素比较
}

  • The above code has obvious shortcomings, so let's simplify it.
for(vp = &values[m-1]; vp >= &values[0];vp--)
{
    
    
  *vp = 0;
}
  • In fact, the task can be successfully completed on most compilers, but we should still avoid writing this way, because the standard does not guarantee that it will work.
  • standard regulation:
  • A pointer to an array element is allowed to be compared with a pointer to a memory location after the last element of the array, but not with a pointer to a memory location before the first element.

3. Pointers and arrays

  • Let's first use an example to establish the connection between arrays and pointers
#include <stdio.h>
int main()
{
    
    
int arr[10] = {
    
    1,2,3,4,5,6,7,8,9,0};
  printf("%p\n", arr);//打印地址
  printf("%p\n", &arr[0]);
  return 0;

insert image description here

int arr[10] = {
    
    1,2,3,4,5,6,7,8,9,0};
int *p = arr;//p存放的是数组首元素的地址
  • Since the array name can be stored as an address in a pointer, we can use a pointer to access an array.
  • Examples are as follows:
#include <stdio.h>
int main()
{
    
    
  int arr[] = {
    
    1,2,3,4,5,6,7,8,9,0};
  int *p = arr; //指针存放数组首元素的地址
  int sz = sizeof(arr)/sizeof(arr[0]);
  int i;
  for(i=0; i<sz; i++)
 {
    
    
    printf("&arr[%d] = %p  <====> p+%d = %p\n", i, &arr[i], i, p+i);
    }
    return 0;
 }

insert image description here

  • So p+i actually calculates the address of the subscript i of the array arr.
    Then we can access the array directly through the pointer
    .
  • Examples are as follows:
int main()
{
    
    
int arr[] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int *p = arr; //指针存放数组首元素的地址
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
for (i = 0; i<sz; i++)
{
    
    
printf("%d ", *(p + i));
}
return 0;
}

Four. Secondary pointer

  • A pointer variable is also a variable, and a variable has an address. Where is the address of the pointer variable stored?
  • The answer is self-evident - secondary pointer.

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

insert image description here

  • We can see that the values ​​of the three are the same! !

  • The operations on secondary pointers are:

 //*pa通过对pa中的地址进行解引用,这样找到的是p ,*pa 其实访问的就是p .
int b = 20;
*pa = &b;//等价于 p = &b;
//**pa 先通过* pa 找到 p, 然后对 p 进行解引用操作:*p ,那找到的是 a .
**pa = 30;
//等价于*p = 30;
//等价于a = 30;

5. Array of pointers

  • Is an array of pointers a pointer or an array?
  • is an array. is an array of pointers.

数组我们已经知道整形数组,字符数组。

int arr1[5];
char arr2[6];
  • What about an array of pointers?
int* arr3[5];//是什么?
  • arr3 is an array with five elements, each element is an integer pointer.
    insert image description here
  • There is also a pointer type called array pointer, which is a pointer. I will introduce these two cases in the advanced chapter of pointers.

Summarize

  • Today's content is all over here. We have introduced all the basic parts of pointers today, including the definition of wild pointers and how to avoid them, several forms of pointer arithmetic, the relationship between pointers and arrays, and secondary pointers. I hope everyone understands the code in the blog!

  • 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/131585455
Recommended