pointers in C

C pointer

Learning pointers in C is easy and fun. While pointers simplify the performance of some C programming tasks, others, such as dynamic memory allocation, cannot be performed without pointers. Therefore, if you want to become an excellent C programmer, it is necessary to learn pointers.

As you know, every variable has a memory location, and every memory location defines an address that can be accessed using the & operator, which represents an address in memory.

Please see the following example, which will output the address of the variable defined:

Example:

#include <stdio.h>
 
int main ()
{
    int var_runoob = 10;
    int *p;              // 定义指针变量
    p = &var_runoob;
 
   printf("var_runoob 变量的地址: %p\n", p);
   return 0;
}

When the above code is compiled and executed, it produces the following result:

var_runoob 变量的地址: 0x7ffeeaae08d8

insert image description here

When a function needs to return multiple results

Since the C language uses the value passing method to pass the parameter value to the called function, the value in the main function cannot be modified in the called function in the C language. You can pass the address value of the variable in the main function to The called function, so that the value of the variable can be accessed through the address in the called function, and the value of the variable can be changed.

① Example of exchanging function values

void swap(int a,int b){
	int temp;
	temp=a;
	a=b;
	b=temp;
} 

Without using pointer variables, we cannot return the changed values ​​of a and b at the same time; at this time, we can use the pointer as a parameter and access the value of the variable through the address to achieve it.

#include<stdio.h>
void swap(int *a,int *b){
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
} 
int main(){
	int a=1;
	int b=3;
	swap(&a,&b);
	printf("a=%d b=%d",a,b);
	return 0;
}
运行结果:
a=3 b=1
--------------------------------
Process exited after 0.03594 seconds with return value 0
请按任意键继续. . .

It can be seen that the address of the variable is passed into the swap function in the main function. In the swap function, we directly access the value of the variable in the main function through the pointer, and exchange the value, and print out the exchanged value in the main function.

arrays and pointers

There is a great relationship between arrays and pointers in C language. Specifically, let's look down:

①Define an array of a

int a[5]={1,2,3,4,5};

insert image description here
Five objects of int type exist in the above five areas;

② Now we declare a pointer: int *p; and assign a value to the pointer; p=&a[0];

#include<stdio.h>
int main(){
	int a[]={1,2,3,4,5};
	int i=0;
	int *p;       //声明了一个指针变量
	p=&a[0];      //给指针p赋了a[0]的地址值给指针
	for(i=0;i<5;i++){
		printf("%d ",a[i]); //通过for循环将数组里的值打印出来了
	}
	printf("\n");
	for(i=0;i<5;i++){
		printf("%d ",*p);  //第一次循环的时候通过指针访问到了a[0]的值,并输出了
		p++;       //对地址加1。我们第一次对地址+1得到了a[1]的地址,第二次+1得到a[2]的地址.......
	}
	printf("\n");
	return 0;
}
运行结果:
1 2 3 4 5
1 2 3 4 5
--------------------------------
Process exited after 0.04136 seconds with return value 0
请按任意键继续. . .

③Proceed to explore how the address + 1 stored by the pointer p becomes the address of a[1]...

output address

#include<stdio.h>
int main(){
	int a[]={1,2,3,4,5};
	printf("%p\n",&a[0]);
	printf("%p\n",&a[1]);
	return 0;
}
运行结果:
000000000062FE00
000000000062FE04
--------------------------------
Process exited after 0.04234 seconds with return value 0
请按任意键继续. . .

We got the address space of a[0] is 000000000062FE00; the address space of a[1] is 000000000062FE04;

We found that the address value between a[0] and a[1] has a difference of 4; then why the value stored in the p pointer is 000000000062FE00 at the beginning, shouldn’t it be 000000000062FE01 when we +1 it? In fact, we found that what we got was the address 000000000062FE04 of a[1].

This is because the p pointer points to a[0] in the array of int type; when performing p++, we + a sizeof(int)=4; so *(p+1) points to a[1 ]; Then if it is an array of char type, when the pointer is p++, 1 is really added; because sizeof(char)=1;

In short, no matter what type the array is, adding one to the pointer means that it points to the next object in the array;

④ Pointer and array relationship

General output:

#include<stdio.h>
int main(){
	int a[]={1,2,3,4,5};
	int i=0;
	for(i=0;i<5;i++){
		printf("%d ",a[i]);
	}
	return 0;
}
运行结果:
1 2 3 4 5
--------------------------------
Process exited after 0.04031 seconds with return value 0
请按任意键继续. . .

Pointer output:

#include<stdio.h>
int main(){
	int a[]={1,2,3,4,5};
	int i=0;
	for(i=0;i<5;i++){
		printf("%d ",*(a+i));
	}
	return 0;
}
运算结果:
1 2 3 4 5
--------------------------------
Process exited after 0.03991 seconds with return value 0
请按任意键继续. . .

By comparison, we found that the printf("%d ",a[i]); and printf("%d ", (a+i)); statements are completely equivalent, that is, a[i] can be written as (a+i ), it may be difficult to understand. In fact, when compiling C language, it is first converted into *(a+i) and then the calculation is performed. In fact, we can also understand it in another way, address &a[i] and address ( a+i) are the same.

Now we find that arrays and pointers are similar things, but it is worth noting that a pointer is a variable, an array is not! And when we pass an array into a function, what we actually pass in is the address of the first element in the array'

Guess you like

Origin blog.csdn.net/QYgujingjing/article/details/129906565
Recommended