C pointer


Learning pointers in C language is easy and fun. Pointers simplify the execution of some C programming tasks, and some tasks, 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 each memory location defines an address that can be accessed using the hyphen (&) operator, which represents an address in memory. . Please look at the following example, which will output the defined variable address:

#include <stdio.h>int main (){   int  var1;   char var2[10];

   printf("var1 变量的地址: %x\n", &var1  );
   printf("var2 变量的地址: %x\n", &var2  );   return 0;}

When the above code is compiled and executed, it will produce the following results:

var1 变量的地址: bff5a400
var2 变量的地址: bff5a3f6

Through the above example, we Learn what a memory address is and how to access it. Next let's look at what pointers are.

What is a pointer?

A pointer is a variable whose value is the address of another variable, that is, the direct address of a memory location. Just like any other variable or constant, you must declare a pointer before using it to store the address of another variable. The general form of pointer variable declaration is:

type *var-name;

Here, type is the base type of the pointer, which must be a valid C data type, var-name is the name of the pointer variable. The asterisk * used to declare pointers is the same as used in multiplication. However, in this statement, the asterisk is used to specify that a variable is a pointer. The following is a valid pointer declaration:

int    *ip;    /* 一个整型的指针 */double *dp;    /* 一个 double 型的指针 */float  *fp;    /* 一个浮点型的指针 */char   *ch     /* 一个字符型的指针 */

The actual data types of all pointer values, whether integer, floating point, character, or other data types, are the same and represent the same Long hexadecimal number of memory address. The only difference between pointers of different data types is the data type of the variable or constant pointed to by the pointer.

How to use pointers?

When using pointers, the following operations are frequently performed: defining a pointer variable, assigning the variable address to the pointer, and accessing the value of the address available in the pointer variable. These are performed by using the unary operator * to return the value of the variable located at the address specified by the operand. The following example involves these operations:

#include <stdio.h>int main (){   int  var = 20;   /* 实际变量的声明 */   int  *ip;        /* 指针变量的声明 */

   ip = &var;  /* 在指针变量中存储 var 的地址 */

   printf("Address of var variable: %x\n", &var  );   /* 在指针变量中存储的地址 */
   printf("Address stored in ip variable: %x\n", ip );   /* 使用指针访问值 */
   printf("Value of *ip variable: %d\n", *ip );   return 0;}

When the above code is compiled and executed, it will produce the following results:

Address of var variable: bffd8b3cAddress stored in ip variable: bffd8b3cValue of *ip variable: 20

The NULL pointer in C

is in When declaring a variable, if there is no exact address to assign a value to, it is a good programming practice to assign a NULL value to the pointer variable. A pointer assigned a NULL value is called a null pointer.

NULL pointer is a constant defined in the standard library with a value of zero. Please look at the following program:

#include <stdio.h>int main (){   int  *ptr = NULL;

   printf("ptr 的值是 %x\n", ptr  ); 
   return 0;}

When the above code is compiled and executed, it will produce the following results:

ptr 的值是 0

On most operating systems, the program is not allowed to access the address 0 of memory because this memory is reserved by the operating system. However, memory address 0 has special significance as it indicates that the pointer does not point to an accessible memory location. But by convention, if a pointer contains null (zero value), it is assumed to point to nothing.

To check for a null pointer, you can use the if statement, as shown below:

if(ptr)     /* 如果 p 非空,则完成 */if(!ptr)    /* 如果 p 为空,则完成 */

Detailed explanation of C pointers

In C, there are many pointer-related concepts. These concepts are simple, but important. Listed below are some important pointer-related concepts that C programmers must be aware of:

ConceptDescription
Arithmetic operations on pointersFour arithmetic operations can be performed on pointers :++,--,+,-
Pointer arrayYou can define an array used to store pointers.
Pointer to pointerC allows pointer to pointer.
Pass pointer to functionPass parameters by reference or address so that the passed parameters are changed in the calling function.
Returning pointers from functionsC allows functions to return pointers to local variables, static variables and dynamic memory allocation.