pointers and arrays

Using pointers can make programs more efficient, and pointers can effectively handle arrays. Array representation is actually using pointers in disguise to start learning GO
insert image description here


pointers and arrays

The array name is the first address of the array element

int a[3];
a==&a[0];	//数组名表示数组元素的首地址
#include<stdio.h>
#define SIZE 5
int main()
{
    
    
    int dates[SIZE];
    int *pt1;
    long a[SIZE];
    long *pt2;
    printf("        %s                 %s\n","int","long");
    for(int i=0;i<SIZE;i++)
        printf(" +  %d:  %10p  %10p\n",i,pt1+i,pt2+i);
    return 0;
}

insert image description here
In C, pointer + 1 refers to adding a storage unit
. For arrays, it represents the address of the next element. The
insert image description here
pointer is incremented by 1, and the value of the pointer is incremented by the size (in bytes) of the type it points to.
Assuming a is an integer Array, the following is ✔

a+1==&a[1];		//相同的地址
*(a+2)==a[2];	//相同的值

You can see that arrays and pointers are like 好基友, ahem, you can use pointers to represent the elements of the array and get the value of the array elements

Note: The indirection operator * has higher precedence than the arithmetic operator +

*(a+2);		//a的第三个元素的值
*a+2;		//a的第一个元素的值加2

数组表示Learning this, we can choose and when writing code 指针表示, we can use pointers to represent arrays, and conversely, we can also use arrays to represent pointers, we should pay attention when using functions that take arrays as parameters


function arrays and pointers

Suppose there is a need, and you need a function that handles arrays that returns the sum of all elements in the array

Idea:
Iteratively calculate the sum by reading the array data once in a loop, you need to master macro definitions, custom functions, and array and loop structures
Function prototype:

//函数原型
int sum(int arr[],int n);

Function definition:

//函数定义
int sum(int arr[],int n)
{
    
    
    int total = 0;
    for(int i=0;i<10;i++)
    total+=arr[i];
    return total;
}

function call:

//函数调用
    int answer = sum(sd,SIZE);

Full code:

#include<stdio.h>
#define SIZE 10
int sum(int arr[],int n);
int main(void){
    
    
    int sd[SIZE] = {
    
    10,20,30,40,50,60,70,80,90,100};
    int answer = sum(sd,SIZE);
    printf("%d\n" ,answer);
    printf("%zd",sizeof sd);
    return 0;
}
int sum(int arr[],int n)
{
    
    
    int total = 0;
    for(int i=0;i<10;i++)
    total+=arr[i];
    return total;
}

insert image description here


Using pointer parameters

When working with arrays with functions, you must know when to start and when to end. In addition to the above method passing parameters, we can also pass two pointers, the first pointer points to the beginning of the array, and the second pointer points to the end of the array. For example the following:

#include<stdio.h>
#define SIZE 10

int sum(int *start,int *end);
int main()
{
    
    
    int a[SIZE]={
    
    10,20,30,40,50,60,70,80,90,100};
    int b=sum(a,a+SIZE);
    printf("%d\n",b);
    return 0;
}

int sum(int *start,int *end){
    
    
   int a=0;
   while(start<end){
    
    
       a+=*start;
       start++;
   } 
   return a;
}

insert image description here


pointer manipulation

What operations can C perform on pointers? Let's learn some basic pointer operations

1. Assignment: You can assign an address to a pointer. For example: assignment using an array name, a variable name of the address operator &, another pointer, etc.
The address should be compatible with the type of the pointer, that is to say, the address of the int type is given to the pointer of the int type. C99/C11 has enforced that type incompatibility is not allowed.

int a[3]={
    
    1,2,3};
int *ptr,*ptr1;
ptr=a;
ptr1=&a[1];

This is to assign the first address of the integer array a to the integer pointer ptr, and assign the second address of the integer array a to the integer pointer ptr1

2. Dereference and value: The * operator gives the value stored at the address pointed to by the pointer.
Dereference:

#include<stdio.h>
int main(){
    
    
    int a[3]={
    
    1,2,3};
    int *ptr,*ptr1;
    ptr=a;
    ptr1=&a[1];
    printf("%d\n",*ptr);
}

insert image description here
Value: Like all variables, pointer variables have their own addresses and values. For AND pointers, the & operator gives the address of the pointer value,

#include<stdio.h>
int main(){
    
    
    int a[3]={
    
    1,2,3};
    int *ptr,*ptr1;
    ptr=a;
    ptr1=&a[1];
    printf("ptr1=%p,*ptr1=%d,&ptr1=%p\n",ptr1,*ptr1,&ptr1);
}

insert image description here

The three parameters ptr1 used here refer to the address of the pointer, *ptr1 dereferences the value pointed to by the pointer, and the address of the value pointed to by the &ptr1 pointer

3. Increment decrement pointer: A pointer to an array element allows the pointer to move to the next element of the array. Decreasing, of course, is going back again

int a[]={
    
    1,2,3};
int ptr1;
ptr1=&a[1];
ptr1++;//让指针移动至数组的下一个元素a[2],即递增四个字节
ptr--;//让指针移动至数组的上一个元素a[1],即递减四个字节

4. Pointer difference: You can calculate the difference between two pointers, you can subtract one pointer from another pointer to get an integer, or subtract an integer from one pointer to get another pointer

Note: Never dereference an uninitialized pointer

int *pt1;//未初始化的指针
*pt=5;//严重错误

This stores 5 at the location pointed to by pt, but pt is not initialized, and its value is a random value. Fortunately, there is no problem, or it will erase data or code, or even cause the system to crash.
Remember: when a pointer is created, the system only allocates memory for storing itself, not memory for storing data, so before using the pointer, it must be initialized with the allocated address

int a[3];
int *pt1,*pt2;

Based on these operations, C programmers create arrays of pointers, function pointers, arrays of pointers to pointers, arrays of pointers to functions, etc.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324320572&siteId=291194637