C Pointers and usage examples

  • 2020-06-01 10:25:29
  • OfStack

One of the most frustrating aspects of learning C for beginners is Pointers, which are very useful in C. So let me write down a little bit of my understanding of pointer 1 with the question.

What is a pointer ?

The pointer itself is a variable that stores the address of the data in memory rather than the value of the data itself. It is defined as follows:


 int a=10,*p;  
 p=&a
 int a=10;
 int *p=&a;

First of all, we can understand that int* is to define a pointer p, and since this pointer stores the address, we need to fetch the address of a ( & ) assign the value to p, which means the pointer p points to a.

Many beginners will be confused by these two definitions, but they both mean the same thing. The first definition defines the int type variables a and the pointer p, and then assigns the address of a to p. The second is to assign the address of a to p while defining p. Let's assume that "int *" is the flag that defines the pointer.

What do Pointers do?

This allows us to use *p to find the address of the variable a to which the pointer is pointing, and then manipulate the value in the address (the value is 10).


// Follow the code above 

printf("%p",p)   // As a result, 1 An address (p Pointing variable a The address of the ) . 
printf("%d",*p)  // As a result, 10 , the variable a The value of the. 
printf("%d",&p)  // As a result, 1 An address ( Pointer to the p Because the pointer is also 1 The variables themselves have addresses )

What's the difference between an array name and a pointer?

The array name is an address, which can be interpreted as a pointer (it can only point to the address of the first element of the array). Pointers can refer to other variables and so on.


int str[5]={1,2,3,4,5};
int *p=str;
printf("%d",*p);  // The output is 1 , the first element of the array. 
printf("%d",*str); // The output is 1 , the first element of the array. 
printf("%d",str[0]); // The output is 1 , the first element of the array. 
printf("%p",p); // The output is the address, the address of the array. 
printf("%p",str); // The output is the address, the address of the array. 
printf("%d",*(p+1)); // The output is 2 , the first of the array 2 An element. 
printf("%d",*(srt+1)); // The output is 2 , the first of the array 2 An element. 

You can clearly see from the code that the array name does exactly the same thing to the data stored in memory, but the array name stores the first address of the array itself.

What is a null pointer, a wild pointer?

After assigning the pointer inside the function, the pointer becomes a null pointer (the variable pops out of the stack) after the function completes execution. But Pointers allocated via dynamic addresses (heap space) don't have the same problem.

The pointer that frees memory after free(p) is a wild pointer.

About the limitation of const on Pointers


int a=10,b=20;
int * const p=&a;  // Can't make p Point to another address. 
p=&b;          // Such operations are not allowed. 
*p=20;          // You can do it like this. 

const int * p=&a;   //p You can point to other addresses. 
p=&b;           // It can be operated. 
*p=10;           // Operation is not allowed. 

const int * const p=&a  //p You cannot point to another address, nor can you change the value in the address. 
p=&b;           // Operation is not allowed. 
*p=20;           // Operation is not allowed. 

const is on the left of int and that means you can't change the value in the address, and on the right you can't point to any other address, both left and right and that means you can't change the value of the address and you can't point to any other address.

Beginners often appear in the problem I personally summed up the above points, if there are mistakes in the hope that we communicate and learn from each other. We'll write about the storage of multidimensional arrays and Pointers.

Here is a brief overview of several USES of Pointers in the c language.

1. Pointer to variable:
Here is a code block:


int main (a) 
{int a=10;int b=15;test(a,b);printf("a=%d,b=%d\n",a,b);}
void test(int x,int y)
{int tmp;tmp=x;x=y;y=tmp;}

The final output is a=10,b=15. Because when a function is called, only values are passed between arguments and parameters. However, when we use Pointers, the results are different, such as:


int main (a) 
{int a=10;int b=15;test(&a,&b);printf("a=%d,b=%d\n",a,b);return 0;}
void test(int * x,int *y)
{int tmp;tmp=*x;*x=*y;*y=tmp;}

Output a=15,b=10. The values of the variables a and b are swapped. This is because we use Pointers to access the storage location of the variable and indirectly modify the value of the variable.

2. Pointer to array:

Define an array and initialize it, int array[5]={2,5,12,7,8}, define a pointer variable and assign the address of the array to it, int *p=array, notice that the array name is the address of the array, and the address of the array is the address of the first element. So our pointer variable points to the first element of the array, *p=2. If (p+1) is used, the pointer variable points to the next element of the array, 5, so we can use the pointer to traverse the elements of the array:


int main()
{int array[5]={2 . 5 . 12 . 7 . 8};int *p =array;for(int i=0;i<5;i++){printf("array[%d]=%d\n",i,*(p+i));}return 0;}

3. Pointer to a string:

We all know that we use arrays to store strings, char name[20]="jack", so we can do this, char *name="jack", the pointer variable points to the first character of the string and can access each character of the string in turn.

4. Pointer to function:

We need to know how to represent a pointer to a function, which means that the syntax should be correct. Here I will also take a code block to illustrate 1:


int sum(int x,int y)
{return x+y;}
int main()
{int a=5;int b=6;int (*p)(int,int);p=sum;int result=(*p)(a,b);printf("The result is %d\n",result);return 0;}

It is not difficult to see that the statement (*p) (a,b) in the code block above can be replaced by p(a,b), because p and sum are the same, although it is easier to understand a point with the former. And we need to know how to define a pointer to a function, int (*p)(int,int), that's the way to write it, int is the type of the return value of the function that the pointer is going to point to, void if there's no return value, void, int in parentheses is of course the formal parameter of the function that the pointer is going to point to. Pointers to functions can be a bit abstract, so practice them if you want to master them.

5. Pointer to structure:

Let's first define a structure type,


struct student
{
   char *name;
   int ages;
};

Then, the structure variable struct student stu={"Rose",15} is defined according to the type. Define 1 pointer to the structure type, struct student *p; Assign the address of the struct variable stu to the pointer variable p,p= & stu; There are three ways to access the property ages in a structure:

stu.ages=15;(*p).ages=15;p- > ages = 15; However, the third way in C can only be used to point to structures.

In summary, the basic use of the pointer is these several, there are some unusual use of this site here on 11 examples, you can go to browse the relevant materials.

conclusion

That's all for the C Pointers and usage examples in this article. Thank you for your support!


Related articles: