C language pointers-first sight of pointers

The best time to plant a tree is ten years ago, followed by now.

Please point out any errors, thank you~

One, pointer variable

Before talking about pointer variables, let me talk about the address . In most computers nowadays, the memory is divided into bytes , and each byte can store 8 bits of information, such as 01010101, and each byte has a unique address .

Some variables occupy one or more bytes of memory space, and the address of the first byte is regarded as the address of this variable.

1. The pointer variable is a variable that stores the address of the variable.

2. Pointer variable declaration:

In the past, for example, we declared an integer variable, int a; Now let’s talk about declaring a pointer variable. Generally, when declaring a pointer variable, we use p (it comes from the first letter of the English word point), a pointer that represents an integer variable. It declares int *p like this ;  , It can also be said that the pointer p is a pointer variable pointing to an object of type int. For int p; our understanding is that an integer variable is declared, and now an * sign is added in front of this integer variable, which indicates that the declared p is a pointer variable.

2. & and *: Take address operator and indirect addressing operator.

Do you still remember the sentence scanf("%d",&a); that you encountered when you started learning C language? You must be very familiar with & this thing!

1. Take address operator &

As the name suggests, such as &a, is to take out the address of variable a.

The pointer variable is a variable that represents the address of the variable. For the initialization of the pointer variable, we use the address symbol.

2. Pointer variable initialization

The pointer p must be initialized before using the pointer p !

//方法一:
int i, *p;
p = &i;

//方法二:
int i;
int *p = &i;

//甚至也可以这样:
int i,*p = &i;

3. Indirect addressing operator * 

This * (indirect addressing) operator should not be confused with int *p; *!

When the pointer p variable points to the object, you can use *p to access the content stored in the object.

Only p points to i, and *p is the alias of i .

E.g:

int a = 3;
int *p = &a;
printf("%d",*p);

The output of this code is the content of variable a 3.

Three, pointer assignment

#include<stdio.h>
int main()
{
	int i = 0,j = 1;
	int *p = &i,*q = &j;
	//至此我们定义并初始化了两个整形变量和两个指针,p指向i,q指向j

	*p = 3;
	*q = 4;
	//对指针进行赋值,因为指针直接指向变量的地址,所以对*p *q进行赋值时就直接会改变他们指向的变量的值
	//因此到这里  i为3, j为4 

	printf("*p = %d\n*q = %d\n",*p,*q);
	printf("i = %d\nj = %d\n",i,j);
	
	return 0;
}

The result is as follows

Pointer assignment code result

Fourth, the pointer is the parameter

1. We all know that when passing parameters to a function, the value of the actual parameter will not change. So for example, if we want to change the value of an actual parameter in a function, what should we do? Return it and then re-assign it in the main function? But what if you want to change two, three, or four?

For example, to write a function that exchanges values, a = 10; b = 3; c = 9; to exchange the values ​​of these three variables two by two, the exchange is a = 3; b = 9; c = 10; write one like this If we simply pass in the three parameters of abc, after the internal exchange of the function, they will still be the original value after the function.

#include<stdio.h>
int swap(int a,int b,int c);

int main()
{
	int a = 10,b = 3, c = 9;
	swap(a,b,c);
	printf("a=%d b=%d c=%d \n",a,b,c);
	
	return 0;
}

int swap(int a,int b,int c)
{
	int t;
	t = a;
	a = b;
	b = c;
	c = t;
	return 0;
}

After running, you will find that his results have not changed, what should I do?

Use pointers as parameters! Because the pointer can point to the address of this variable, you can use the assignment of *p to directly change the value of the variable.

#include<stdio.h>
int swap(int *a,int *b,int *c);

int main()
{
	int a = 10,b = 3, c = 9;
	swap(&a,&b,&c);
	printf("a=%d b=%d c=%d \n",a,b,c);
	
	return 0;
}

int swap(int *a,int *b,int *c)
{
	int t;
	t = *a;
	*a = *b;
	*b = *c;
	*c = t;
	return 0;
}

Go and run it for a while~

 

2. In this way, our pointer has one more application scenario: a function can only have one return value, and when multiple results are needed, we can use pointers to bring out multiple results.

Of course, when an error-prone operation uses -1 or 0 as the return value to indicate a failure, if there is no error, we can also use a pointer to send the result.

3. Try const?

const defines variables, constant variables. The value of a variable defined with const is not allowed to be changed. It can be said that it defines a read-only variable. This also means that it must be assigned an initial value when it is defined.

We can protect parameters with const

Because sometimes we pass in a pointer parameter and don't want it to be changed, so just const it. (When the variable needs a lot of storage space, passing the value of the variable at this time will waste time and space)

 

 

These are the most basic things about pointers, followed by pointers and arrays, dynamic memory allocation, and so on.

Come on together~!

 

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/115011435