Basic Concepts C language - the pointer (a)

What is a pointer?

Pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like other variable or constant, must address before using other variables pointer storage, it is declared. The general form of the pointer variable declaration is:
type *var-name;
type is a base type pointer, it must be a valid C data type, var-name is the name of a pointer variable.

How to use the pointer

Define a pointer variable, the variable is assigned to the address pointer, to access the value of the pointer variable available addresses.

#include <stdio.h>
 int main ()
{
   int  var = 20;   /* 实际变量的声明 */
   int  *ip;        /* 指针变量的声明 */
   ip = &var;  /* 在指针变量中存储 var 的地址 */
   printf("Address of var variable: %p\n", &var  );
   /* 在指针变量中存储的地址 */
   printf("Address stored in ip variable: %p\n", ip );
   /* 使用指针访问值 */
   printf("Value of *ip variable: %d\n", *ip );
   return 0;
}

Compiled results are as follows

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

c is NULL pointer

When variable declaration, if not the exact address can be assigned, is assigned a NULL pointer variable. Fu NULL pointer value is called a null pointer.

#include <stdio.h>
 
int main ()
{
   int  *ptr = NULL;
 
   printf("ptr 的地址是 %p\n", ptr  );
 
   return 0;
}

operation result

ptr 的地址是 0x0

0 indicates that the memory address pointer does not point to a memory location accessible. However, by convention, if the pointer contains a null value (zero value), it is assumed that it does not point to anything.

Published 38 original articles · won praise 65 · views 4938

Guess you like

Origin blog.csdn.net/Miracle1203/article/details/104224421
Recommended