Let's summarize the pointers of C language (basic edition)

I recently found out that pointers are also used in Go language, so I'm not sure if it can be applied. Let's summarize the pointers in C language that I used before.

What is a pointer in the first place?

A variable that stores the memory address of the variable. Variables and functions are stored in memory, and variables that have information indicating where they are located are pointer variables.

○ Example: Relationship between address and value when a variable is declared

/*Variable declaration*/
int A;
/*Declaration of pointer variables*/
int* pA;
/*Assign a value to a variable*/
A = 10;
/*Store the address of A in the pointer*/
pA = &A;

With the above contents, the value is stored in the memory in the following form. スクリーンショット 2020-07-27 14.15.58.png In the case of the above figure, '10' is stored in `0xXXXX4 ~ 0xXXXX7``` when the value is stored in the variable (the fact that multiple addresses are secured here will be explained later. ) In the example, the address of'''A''' is assigned to the pointer variable'''pA''', so `0xXXXX4``` is entered in'''pA'''.

Basic usage of pointers

You can check the address of the memory by using the pointer, and you can also operate the value of the address.

○ Each operation method of the pointer

/*Get the address of a variable*/
/* &You can get the address of the target variable by adding to the beginning of the variable.*/
/*In the following cases, assign the address to the pointer variable pA*/
pA = &A;
/*Pointer destination(Address destination)To access*/
/* *You can access the address stored in the variable by adding to the beginning of the variable.*/
/*In the following cases, the value of the address destination stored in the pointer variable pA is rewritten to 20.*/
*pA = 20;

The figure below shows the above. スクリーンショット 2020-07-27 14.31.08.png This is the basis of pointers. From here, I would like to continue as an article such as points to note about pointers and advanced editions.

Recommended Posts