Intentional Arrangement of Pointer in [C Language]

A simple introduction to pointers

1. Pointer: A special variable in which the stored value is interpreted as an address in memory.
2. Definition of pointer: Address data type +* + pointer name;

int * ptr;
char * ptr;
float * ptr;

3. Pointer size: 32-bit platform is 4 bytes, 64-bit platform is 8 bytes;
4. Derivation of pointers: borrow the * operator to refer to the variables indicated by the pointer;

int a = 15;
int * ptr = &a;//Assign the address of a to ptr
* ptr = 10 - *ptr;  //Equivalent to a = 10 - a;
printf("%d" , *ptr);//The ptr is de-referenced, i.e. the value of a is printed.

5. Illegal operation:

  • Uninitialized pointer derreferences are prohibited.
  • NULL pointer can not be accessed;

6.NULL pointer: the pointer points to an empty address

	int *ptr = NULL;//The two statements are equivalent.
	int * ptr = 0;

7. Pointer Constant: Dereference only works for pointer type expressions.
Not at all. Each run assigns a random address.

*(int *)100=25; //Assuming that a variable a is stored at 100 locations, it can be de-referenced only if the pointer type of 100 is strongly changed.

8. Secondary pointer

	int a = 15;
	int* ptr = &a;
	int **c = &ptr;//c Save the address of ptr
	printf("%d", **c);//Print a value

9. Operation of Pointer

	char *ptr = "hello,world";
	printf("%p\n", ptr);
	printf("%c\n", *ptr);
	printf("%p\n", ++ptr);//Move the pointer position backwards to the char type bit
	printf("%c\n", *ptr);
	printf("%c\n", *++ptr);//From right to left, first position increases, then dereference

Pointer and Array

The array name represents the address of the first element of the array, and can be used as a pointer.
There are only two exceptions:

  • sizeof (array name), representing the size of the entire array.
  • & Array name, which represents the first address of the entire array
	int a[10],num;
	num = sizeof(a) / sizeof(a[0]);
	printf("%d\n", num);
	printf("%p\n", &a);//Represents the array header address
	printf("%p\n", a);
	printf("%p\n", &a + 1);//Represents the address of the next array
	printf("%p\n", a + 1);

Array pointers: pointers to arrays

	int a[10];
	int(*ptr)[10] = a;

Pointer array: An array used to store pointers

int *ptr[10];//Store Integer Pointer
char *ptr2[10];//Store Character Pointer

Array parametric correlation:

One-dimensional array parameter transfer:

void fun1(int a[]);//Equivalent to int*a
void fun3(int *a);//Integer pointer substitution
void fun3(int *a[5]);//Integer pointer array substitution

Two-dimensional array parameter transfer:

void fun(int b[][3]);//The first parameter can be omitted and the second can not be omitted.
void fun(int ** b);//Second-level pointer for reference
void fun3(int (*b)[5]);//Array pointer for two-dimensional array parameter transfer

3. Function pointer

void (* pfun)();//pfun can store the address of the function.

Understand the following code:

void(*signal(int, void(*)(int)))(int);

Simplified code

typedef  void(*pfun)(int);
pfun signal(int, pfun);

Function pointer array:
Array for storing multiple function pointers

int (*p[4])(int x,int y)={add,sub,mul,chu};
int ret = (*p[2])(x,y);//Call the sub function and pass the return value to ret

Added by al3x8730 on Mon, 07 Oct 2019 04:20:19 +0300