当前位置:网站首页>C secret arts script Chapter 2 (detailed explanation of pointer) (Section 1)

C secret arts script Chapter 2 (detailed explanation of pointer) (Section 1)

2022-06-12 14:24:00 Mortal programming biography

After learning from the previous chapter , You must have gained a lot . So this chapter mainly takes you to c A difficult chapter of language , That's the pointer .

1. The concept of pointer : A pointer is a variable , It's used to store the address .

2. The size of the pointer : stay 32 Bit platform unified bit 4 Bytes , stay 64 The bit platform pointer is unified as 8 Bytes .

3. The type of pointer : Yes int* ,char*,float* wait , And they can store different types of addresses . The following code :

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

There is no grammatical problem with this kind of writing , So many people want to know , Since any pointer type can store different types of addresses , So why distinguish between types ? because , The pointer type determines the length of bytes that can access the pointer storage address ,int* Type can access the stored address 4 Bytes , and char* Can access the stored address 1 Byte length , And so on .

Two 、 Character pointer

        seeing the name of a thing one thinks of its function , Is a pointer variable used to store characters , But it seems simple , But there are also many potholes , The following code :

#include<stdio.h>
int main()
{
const char* pstr="hello world";
printf("%s",pstr);
return 0;
}

This code is particularly easy to think of as putting a string into a character pointer pstr in , But the essence is to put the string hello world, The address of the first character is put in pstr in .

3、 ... and 、 Classic title

example 1:

#include<stdio.h>
int main()
{
char arr1[]="abcdef";
char arr2[]="abcdef";
char* p1="abcdef";
char* p2="abcdef";
if(arr1==arr2)
{
    printf("hehe\n");
}
else
{
    printf("haha\n");
}
return 0;
}

Is it output hehe still haha Well ?

analysis : We created 2 A character array and a character pointer are stored abcdef,arr1==arr2?, Although the strings stored in the array are consistent , But neither of them belongs to the same memory space , In other words : Although you are the same as your neighbor , But is the house number the same ? So the result is printed haha. Then, let's change the result of the establishment conditions ?

#include<stdio.h>
int main()
{
char arr1[]="abcdef";
char arr2[]="abcdef";
char* p1="abcdef";
char* p2="abcdef";
if(p1==p2)
{
    printf("hehe\n");
}
else
{
    printf("haha\n");
}
return 0;
}

p1 and p2 It's all pointers , At the same time point to abcdef It also means that the memory address is the same, so the equality condition holds , Print hehe.

Four 、 Pointer array

1. Concept : It's essentially an array , It's not a pointer , Is an array used to store pointers

Such as :

#include<stdio.h>
int main()
{
int*arr[10];
return 0;
}

2. Basic usage of pointer array

#include<stdio.h>
int main()
{
int arr1[]={1,2,3,4,5};
int arr2[]={2,3,4,5,6};
int arr3[]={3,4,5,6,7};
int* paar[]={arr1,arr2,arr3};
int i=0;
for(i=0;i<3;i++)
{
    int j=0;
    for(j=0;j<5;j++)
    {
        printf("%d ",*(parr[i]+j));
    }
}
return 0;
}

      This code uses the pointer array to save three arrays , Be careful : Because the address of the first element is stored in the pointer array ( Array name ), So traversing the array is a loop .

5、 ... and 、 Array pointer

1. Concept : As opposed to pointer arrays , It's essentially a pointer , Is a pointer to an array .

Such as :

#include<stdio.h>
int main()
{
int arr[10]={0};
int (*pa)[10]=&arr;     // Array pointer 
return 0;
}

*pa Enclosed in parentheses because [] The priority ratio * high , This array pointer means : It's a pointer , This pointer points to an array , The array has ten elements , Each element type is int, It's an array pointer .

2. Usage of array pointers :

#include<stdio.h>
void print(int(*p)[5],int x,int y)
{
int i=0;
for(i=0;i<x;i++)
{
    int j=0;
    for(j=0;j<y;j++)
    {
    printf("%d ",*(*(p+i)+j));    
    }
  printf("\n");    // Inner loop traverses the array once and then wraps the line 
  }
}
int main()
{
int arr[3][5]={
   {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}};    // Create a 3 That's ok 5 Two dimensional array of columns 
print(arr,3,5);    // Pass the address of the first element of the two-dimensional array and the number of rows and columns .
return 0;
}

Dereference notes : The dereference first finds the address of the row of the two-dimensional array, and then adds j To traverse the address of the column and dereference the address of the two-dimensional array column will traverse the two-dimensional array. This dereference writing method is difficult for novices to understand, so there are other writing methods .

#include<stdio.h>
void print(int(*p)[5],int x,int y)
{
int i=0;
for(i=0;i<x;i++)
{
    int j=0;
    for(j=0;j<y;j++)
    {
    printf("%d ",p[i][j]);    
    }
  printf("\n");    // Inner loop traverses the array once and then wraps the line 
  }
}
int main()
{
int arr[3][5]={
   {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}};    // Create a 3 That's ok 5 Two dimensional array of columns 
print(arr,3,5);    // Pass the address of the first element of the two-dimensional array and the number of rows and columns .
return 0;
}

Dereference notes : Because it is to traverse a two-dimensional array , The first element address is used for parameter transmission , So it can be written in the form of traversal array , As for why we can traverse like this , This involves knowledge of the underlying assembly code , I won't give you a detailed answer here .

原网站

版权声明
本文为[Mortal programming biography]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010512215300.html