catalogue
- I Pointer introduction
- II Pointer type
- III Declare and initialize a pointer
- IV View pointer address and pointer value
- V NULL pointer – NULL pointer
- Vi Key summary
- VII Guess you like it
Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language
I Pointer introduction
Pointers and arrays are the soul of C language array , we have finished the introduction. Before explaining pointers, we need to understand three concepts: memory / memory address / pointer;
1. Memory
C language All variables are stored in memory, just like the water in the cup. Water is a variable, and the cup is memory. Memory is used as a container to store variables;
2. Memory address
Memory address is like a person who has an ID card in addition to his name. Everyone's ID card is unique, and the memory address is also unique. How to view the memory address is also explained in the following article;
3. Pointer declaration
Pointer It is also a variable in C language, and its value is one variable Is the direct address of the memory location. The general form of pointer variable declaration is:
/* type:Pointer type, which must be a valid C data type, for example: int / float / double / char var-name : The name of the pointer variable */ type *var-name;
Note: don't forget that there is a * sign between the pointer type and variable name. Without this * sign, only a common variable is defined, which is also one of the differences between common variable declaration and pointer declaration;
int *p; /* An integer pointer */ int p; /* An integer variable */
II Pointer type
Pointers are flexible and can point to any type of data. Pointer The type of indicates the memory of the address space it points to. The following are valid pointer declarations:
int *p; /* An integer pointer */ double *p; /* A double pointer */ float *p; /* A floating point pointer */ char *p; /* A character pointer */
Note: don't forget that there is a * sign between the pointer type and variable name. Without this * sign, only a common variable is defined, which is also one of the differences between common variable declaration and pointer declaration;
III Declare and initialize a pointer
1. Declare pointer and initialize directly - Recommended
int *p = 10; //Declare a pointer of type int to the memory address with variable 10 double *p = 10.55; //Declare a pointer of type double to the memory address with variable 10.55 float *p = 10.0; //Declare a pointer of float type to the memory address with variable 10.0 char *p = "123456"; //Declare a pointer of char type to the memory address with variable "123456"
2. Declare the pointer before initialization - not recommended
In C language, when defining local variables, if they are not initialized, the values are random; The initial value of global variable and static variable is 0;
IV View pointer address and pointer value
In the article Format controller / placeholder Many placeholders have been introduced in, and the placeholders for pointers are represented by% p or% x. refer to the following examples for specific use:
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com com //@File:C language tutorial - C language pointer declaration and definition //@Time:2021/06/12 08:00 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include "stdafx.h" #include "stdio.h" #include "stdlib.h" int main() { int iValue = 10; int *pValue = &iValue; printf("Shaping variable iValue Value:%d Shaping variable iValue Memory address:%p\n", iValue,&iValue); printf("Integer pointer variable pValue Value:%d Integer pointer variable pValue Memory address:%p\n", *pValue, pValue); printf("-------------------------------------------------------\n"); *pValue = 20; printf("Shaping variable iValue Value:%d Shaping variable iValue Memory address:%p\n", iValue, &iValue); printf("Integer pointer variable pValue Value:%d Integer pointer variable pValue Memory address:%p\n", *pValue, pValue); printf("-------------------------------------------------------\n"); iValue = 30; printf("Shaping variable iValue Value:%d Shaping variable iValue Memory address:%p\n", iValue, &iValue); printf("Integer pointer variable pValue Value:%d Integer pointer variable pValue Memory address:%p\n", *pValue, pValue); system("pause"); return 0; } /* Output results: Integer variable iValue value: 10 integer variable iValue memory address: 004FF798 Integer pointer variable pValue value: 10 integer pointer variable pValue memory address: 004FF798 ------------------------------------------------------- Integer variable iValue value: 20 integer variable iValue memory address: 004FF798 Integer pointer variable pValue value: 20 integer pointer variable pValue memory address: 004FF798 ------------------------------------------------------- Integer variable iValue value: 30 integer variable iValue memory address: 004FF798 Integer pointer variable pValue value: 30 integer pointer variable pValue memory address: 004FF798 Please press any key to continue */
In the above example, an integer variable iValue is declared, and the integer variable iValue is executed using the pointer pValue. According to the output information, it can be concluded that the memory addresses of both are the same;
Since the shaping variable iValue is the same as the memory address of the pointer variable pValue, the pointer pValue will change with the value of the shaping variable iValue, and the value of the shaping variable iValue will change with the value of the pointer pValue. The two are the same, just like you can find you by name, You can also find you by ID number.
V NULL pointer – NULL pointer
When a variable is declared, if no exact address can be assigned, a NULL value is assigned to the pointer variable, and the pointer assigned a NULL value is called** Null pointer**.
A NULL pointer is a constant with a value of zero defined in the standard library. See the following procedure:
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com com //@File:C language tutorial - C language pointer declaration and definition //@Time:2021/06/12 08:00 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include <stdio.h> int main () { int *ptr = NULL; printf("ptr Your address is %p\n", ptr ); return 0; } //Output: the address of ptr is 0x0
On most operating systems, programs are not allowed to access memory with address 0 because it is reserved by the operating system. However, the memory address 0 is particularly important because it indicates that the pointer does not point to an accessible memory location. But by convention, if the pointer contains a null value (zero value), it is assumed that it does not point to anything.
To check for a null pointer, you can use if Statement, as follows:
if(ptr) /* If p is not empty, complete */ { //.... } if(!ptr) /* If p is empty, complete */ { //.... }
Vi Key summary
As a variable, the pointer must have its own address, Placeholder use% p or% x;
int *p = 10; printf(" p : %p",p);// Output address: 004FF798
As a variable, the pointer must have its own value. The placeholder uses% d. to get the value of the pointer, you need to add * in front of the pointer variable, otherwise it is the address of the pointer, for example:
int *p = 10; printf(" p : %d",*p);// Output value: 10 printf(" p : %p",p); // Output address: 004FF798
Pointers are flexible and can point to any type of data. The type of pointer indicates the memory of the address space it points to
VII Guess you like it
- Install Visual Studio
- Install the Visual Studio plug-in Visual Assist
- Visual Studio 2008 uninstall
- Visual Studio 2003/2015 uninstall
- C language format controller / placeholder
- C language logical operators
- C language ternary operator
- C language comma expression
- Differences between sizeof and strlen functions in C language
- C language strcpy and strcpy_s function difference
- C language memcpy and memcpy_s difference
- C language array definition and use
- C language array traversal
- C language array sorting - bubble sorting
- C language array sorting - selection sorting
- C language array sorting - insertion sorting
- C language array sorting - quick sorting
- C language array subscript out of bounds
- C language array memory overflow
- Difference between C language array subscript out of bounds and memory overflow
- C language array length calculation
- C language pointer declaration and definition
No reprint without permission: Ape programming » C language pointer declaration and definition