Primary pointer knowledge points

catalogue

1. What is the pointer?

2. Pointer and pointer type

2.1} pointer + integer

2.2 pointer dereference

3. Field pointer

3.1} cause of formation

3.1.1} pointer not initialized

3.1.2 pointer cross-border access

3.1.3 # release of space pointed by pointer

3.2 # how to avoid wild pointer

4. Pointer operation

4.1 pointer + - integer

4.2 pointer - pointer

4.3} pointer relation operation

5. Pointers and arrays

6. Secondary pointer

7. Pointer array

Inspirational module

Please be a great adult and a more lovely child

1. What is the pointer?

What is the pointer?
Two key points of pointer understanding:
1. Pointer is the number of the smallest unit in memory, that is, the address
2. The pointer in spoken English usually refers to the pointer variable, which is used to store the memory address

Conclusion: pointer is the address. In colloquial language, pointer usually refers to pointer variable.
Then we can understand it this way: memory

Memory is divided into a small memory unit, and the size of a basic memory unit is one byte. (one byte per memory unit) the number of memory unit is the address number - > address - > pointer (the address can point to this memory unit, and the address is called pointer)

#include <stdio.h>
int main()
{
	int a = 10;//Create a variable a in spatial memory (4 bytes) (a has 4 addresses in total, one address per byte),
	int* pa = &a;//Int * is the type of pa (* indicates that it is a pointer, and int indicates that the variable pointing to that a is of type int). Create a pointer variable pa & A takes the starting address
	*pa = 20;//Find a through the pointer and use * pa. changing the value of pa is changing the value of A
	printf("%d ", a);
	return 0;
}

Summary:
Pointer variable, a variable used to store the address. (the values stored in the pointer are treated as addresses).

Pointers are used to store addresses, which uniquely identify an address space.
The size of the pointer (pa in the figure above) is 4 bytes on the 32-bit platform and 8 bytes on the 64 bit platform (whether int * or double * or float *)

How memory unit numbers are generated:

For a 32-bit machine with 32 address lines, it is assumed that each address line generates high level (high voltage) and low power during addressing
Flat (low voltage) is (1 or 0);
Then the address generated by 32 address lines will be:
00000000 00000000 00000000 00000000       
00000000 00000000 00000000 00000001
...
11111111 11111111 11111111 11111111              
There are 32 addresses to the power of 2.
Each address identifies a byte, then we can give (2^32Byte == 2^32/1024KB==
2 ^ 32 / 1024 / 1024MB = = 2 ^ 32 / 1024 / 1024 / 1024gb = = 4G B).

2. Pointer and pointer type


Here we are discussing the type of pointer
As we all know, variables have different types, such as integer, floating point, etc. Does the pointer have a type?

char  *pc = NULL;
int  *pi = NULL;
short *ps = NULL;
long  *pl = NULL;
float *pf = NULL;
double *pd = NULL;

A pointer of type char * is used to store the address of a variable of type char.
A pointer of type short * is used to store the address of a variable of type short.
Pointer of type int * is used to store the address of variable of type int.

But the pointer variable is on a fixed platform, and the size of the pointer variable is also fixed. What's the meaning of the pointer type?

2.1} pointer + integer

The type of pointer determines how far the pointer moves forward or backward (in bytes)

pa refers to int type, so add one and take four bytes

 

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	int* p = &arr;
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(p + i) = i +1;//initialization
	}
	//Print backwards
	int* q = &arr[9];
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *q);
		q--;
	}


	return 0;
}

The printed result is: 10 9 8 7 6 5 4 3 2 1

int arr[10] = { 0 };

char* pc = (char*)&arr;// Cast type

//The cast address will not change / / only the bytes changed during dereference will change,

2.2 pointer dereference

The pointer type determines how many bytes can be changed at a time when dereferencing

F10 - Window - memory (memory condition can be observed)

 

int * can change 4 bytes when dereferencing, double * can change 8 bytes when dereferencing, and char * can change 1 byte when dereferencing (although they are on a fixed platform, the bytes occupied by pointer variables are the same (4 or 8)

3. Field pointer

3.1} cause of formation

3.1.1} pointer not initialized


#include <stdio.h>
int main()
{
int *p;// Local variable pointer is uninitialized and defaults to random value
  *p = 20;
return 0;

}

3.1.2 pointer cross-border access

#include <stdio.h>
int main()
{
  int arr[10] = {0};
  int *p = arr;// The array name is the address
  int i = 0;
  for(i=0; i<=10; i++)
 {
/ / when the range pointed by the pointer exceeds the range of array arr, p is the wild pointer
    *(p+1) = i;
 }
  return 0;
}

3.1.3 # release of space pointed by pointer

The above code is wrong. When the function ends, the memory is released. When printing, pointing to the memory with an address does not necessarily get the value of 1000.

 

The value of 1000 is not obtained in the above figure, which indicates that it is an error code.

3.2 # how to avoid wild pointer

1. Pointer initialization: specify the position pointed to by the pointer. When you don't know who it points to, point to the null pointer , int* p = NULL ,
2. Be careful that the pointer is out of range
3. The pointer points to the space release even if it is set to NULL
4. Avoid returning the address of local variables
5. Check the validity of the pointer before use

Null null pointer, which is essentially equivalent to 0. int* pa = NULL;   *pa = 100; This way of writing is wrong. The code below is correct.

Code display:

#include <stdio.h>
int main()
{
	int* p = NULL;
	if (p != NULL)
	{
		*p = 100;
	}
	return 0;
}

3. The pointer points to the space release even if it is set to NULL

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	p = NULL;//When you don't want to use this pointer variable, you can define it as a null pointer
	return 0;
}

4. Pointer operation

Pointer + - integer
Pointer pointer
Relational operation of pointer

4.1 pointer + - integer

Code display:

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	int* p = &arr;
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(p + i) = i +1;//Pointer + integer operation
	}
	//Print backwards
	int* q = &arr[9];
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *q);
		q--;//Pointer integer operation
	}


	return 0;
}

4.2 pointer - pointer

Pointer - the premise of pointer is that two pointers point to the same space

Code display:

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	printf("%d\n", &arr[0] - &arr[9]);
	printf("%d", &arr[9] - &arr[0]);
	return 0;
}

Commissioning results: - 9

The first byte of [ar0] is a positive byte, so the first byte of [ar0] is a negative byte, so the first byte of [ar0] is a positive byte.

The third method to find the length of string: (the first two methods are written in function recursion. Interested friends can go and have a look)
Code display:

#include <stdio.h>
int my_strlen(char* s)
{
	int* start = s;
	while (*s != '\0')
	{
		s++;
	}
	return s - start;
}
int main()
{
	char arr[] = "abcd";
	int ret = my_strlen(arr);
	printf("%d ", ret);
	return 0;
}

4.3} pointer relation operation

A pointer to an array element is allowed to be compared with a pointer to the memory location after the last element of the array, but not with
A pointer to the memory location before the first element is compared.

5. Pointers and arrays

The array name represents the address of the first element of the array. (except for the two cases, the array chapter is written. If you are interested, you can go and have a look.) the array name is stored as an address in a pointer

Code display:

#include <stdio.h>
int main()
{
  int arr[] = {1,2,3,4,5,6,7,8,9,0};
  int *p = arr; //Pointer to the address of the first element of the array
  int sz = sizeof(arr)/sizeof(arr[0]);
  for(i=0; i<sz; i++)
 {
    printf("&arr[%d] = %p  <====> p+%d = %p\n", i, &arr[i], i, p+i);
 }
  return 0;
}

p+i actually calculates the address of array arr subscript i

int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int *p = arr; //Pointer to the address of the first element of the array
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
for (i = 0; i<sz; i++)
{
printf("%d ", *(p + i));
}
return 0;
}

6. Secondary pointer

The address of the pointer variable is the secondary pointer

#include <stdio.h>
int main()
{
	int a = 10;
	int* pa = &a;
	int** ppa = &pa;//ppa is the second level pointer. / / the multi-level pointer exists

	return 0;
}

How to understand int * * ppa; Int * indicates that the address of ppa pointing to the element is of type int * and the second * indicates that it is a pointer. (multi level pointers are understood in this way)

Code display:

#include <stdio.h>
int main()
{
	int a = 10;
	int* pa = &a;
	int** ppa = &pa;//ppa is the second level pointer. / / the multi-level pointer exists
	**ppa = 20;//*ppa refers to pa.**ppa refers to a, so the value of a changes to 20;
	printf("%d ", a);
	return 0;
}

7. Pointer array

Pointer array is an array that stores pointers.

Shaping array, storing shaping array, character array, is storing character array. Then the integer pointer array is the array that stores the integer pointer.

 int* arr[5];

 

This is the end of the initial pointer.

Keywords: C

Added by mrsocks on Mon, 07 Feb 2022 20:11:28 +0200