Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C pointer


May 11, 2021 C


Table of contents


C pointer

Learning the C language's pointer is simple and fun. P ointers simplify the execution of some C programming tasks, as well as tasks such as dynamic memory allocation, which cannot be performed without pointers. Therefore, to become a good C programmer, it is necessary to learn the pointer.

As you know, each variable has a memory location, and each memory location defines an address that can be accessed using the lyse operator, which represents an address in memory. Look at the following example, which outputs 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 produces the following results:

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

From the example above, we learned what a memory address is and how to access it. Let's take a look at what a pointer is.

What is a pointer?

A pointer is a variable whose value is the address of another variable, that is, the direct address of the memory location. L ike other variables or constants, you must declare other variable addresses before you can store them with pointers. Pointer variable declarations are generally in the form of:

type *var-name;

Here, type is the base type of the pointer, it must be a valid C data type, var-name is the name of the pointer variable. T he asterisk used to declare the pointer is the same as the asterisk used in multiplication. H owever, in this statement, the asterisk is used to specify that a variable is a pointer. Here's a valid pointer declaration:

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

The actual data types of the values of all pointers, whether integer, floating-point, character type, or any other data type, are the same, representing a long heteens of memory addresses. The only difference between pointers of different data types is that the pointers point to different data types for variables or constants.

How do I use pointers?

When using pointers, you frequently do the following: define a pointer variable, assign a variable address to a pointer, and access the value of the available address in the pointer variable. T hese are the values of variables that * address specified by the operans by using the u-operator . The following examples relate to 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 produces the following results:

Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20

The NULL pointer in C

When a variable is declared, it is a good programming practice to assign an NULL value to a pointer variable if there is no exact address to assign. A pointer assigned to an NULL value is called an empty pointer.

The NULL pointer is a constant that defines a value of zero in the standard library. Take a 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 produces the following results:

ptr 的值是 0

On most operating systems, programs do not allow access to memory with address 0 because it is reserved by the operating system. H owever, memory address 0 is of particular importance, indicating that the pointer does not point to an accessible memory location. By convention, however, if the pointer contains an empty value (zero value), it is assumed that it does not point to anything.

To check for an empty pointer, you can use the if statement, as follows:

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

The C pointer is explained in detail

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

concept describe
Arithmetic operation of pointer Four arithmetic operations can be performed on the pointer: ++, -, +,
Pointer array You can define an array used to store pointers.
Pointer to point to pointers C Allows pointers to the pointer.
Transfer pointer to function By reference or address delivery parameters, the transferred parameters are changed in the call function.
Returns the pointer from the function C Allow functions return pointer to local variables, static variables, and dynamic memory allocation.