Initial order of pointer in C language

Initial order of pointer in C language 1. What is the pointer 2. Pointer and pointer type 3. Field pointer 4. Pointer op...

Initial order of pointer in C language

1. What is the pointer
2. Pointer and pointer type
3. Field pointer
4. Pointer operation
5. Pointers and arrays
6. Secondary pointer
7. Pointer array

1. What is the pointer

Beginners have a question, that is, what is the pointer? Simply put, it is through it that we can find the memory unit with it as the address.

The address points to a certain memory space, so the image of the address is called a pointer.

int main() { int a = 10; int* pa = &a; return 0; } //pa is used to store addresses (pointers), so pa is a pointer variable.

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

An address uniquely identifies a space.

The size of the pointer is 4 bytes on 32-bit platforms and 8 bytes on 64 bit platforms.

2. Pointer and pointer type

We know that variables have different types (integer, floating point, character, etc.), but pointers also have different types.

Meaning of pointer type 1:
The pointer type determines how many bytes are accessed at a time (the size of memory accessed) during pointer dereference operation
char * pointer dereference access 1 byte
int * pointer dereference access four bytes

int main() { char* pc = &a; *pc = 0; return 0; }

Meaning of pointer type 2:
The pointer type determines the step size when the pointer ± integer (when the pointer ± integer, skip a few bytes)
int * pointer + 1 skips four bytes
char * pointer + 1 skips a byte

int main() { int a = 10; int* pa = &a; char* pc = &a; printf("%p\n", pa); printf("%p\n", pc); printf("%p\n", pa+1); printf("%p\n", pc+1); return 0; }

3. Field pointer

The wild pointer is that the position pointed to by the pointer is unknown (random, incorrect and unrestricted).

3.1 cause of formation
  1. Pointer not initialized
int main() { int* p;//Local variable pointer is uninitialized and defaults to random value *p = 20;//Use the random value stored in p as the address to find a space that does not belong to our current program //It causes illegal access, p is the wild pointer return 0; }
  1. Pointer out of bounds access
int main() { int arr[10] = 0; int i = 0; int* p = arr; for(i =0; i <= 10; i++) { *p = i; p++;//When the range pointed to by the pointer exceeds the range of array arr, p is a wild pointer } return 0; }
  1. The space pointed to by the pointer is released
int* test() { int a = 10; return &a; } int main() { int* p = test(); printf("%d\n",*p); return 0; }
3.2 how to avoid wild pointer
  1. Pointer initialization
  2. Be careful that the pointer is out of range
  3. Pointer to space release even if set to NULL
  4. Avoid returning the address of a local variable
  5. Check the validity of the pointer before use
int main() { int a = 10; int* p = &a;//Explicitly initialize and determine the direction int* p2 = NULL;//When you don't know where a pointer should be pointing, it can be initialized to NULL return 0; }

4. Pointer operation

4.1 pointer ± integer
#define N_VALUES 5 float values[N_VALUES]; float* vp; for(vp = &values[0]; vp < &values[N_VALUES];) { *vp++ = 0; }
int main() { int arr[10] = ; int* p = &arr[9]; printf("%p\n",p); printf("%p\n",p-2); return 0; }
4.2 pointer - pointer

Pointer - the absolute value of the number obtained by the pointer is the number of elements between the pointer and the pointer

int main() { int arr[10] = { 1,2,3,4,5,6,7,8,9,0 }; printf("%d\n", &arr[9] - &arr[0]); printf("%d\n", &arr[0] - &arr[9]); return 0; }

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

int main() { int arr[10] = ; char ch[5] = ; printf("%d\n",&arr[9] - &ch[0]);//err return 0; }

Apply string length

int my_strlen(char* s) { int count = 0; char* start = s; while(*s!='\0') { s++; } return s - start; } int main() { char arr[] = "abcdef"; int len = my_strlen(arr); printf("%d\n", len); return 0; }
4.3 pointer relation operation
#define N_VALUES 5 float values[N_VALUES]; float *vp; for(vp = &values[N_VALUES]; vp > &values[0];) { *--vp = 0; }

The above procedure can also be written like this

for(vp = &values[N_VALUES-1]; vp >= &values[0];vp--) { *vp = 0; }

In fact, most compilers can successfully complete the task, but we should avoid it because the standard does not guarantee its feasibility.

Standard provisions

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 it is not allowed to be compared with a pointer to the memory location before the first element.

5. Pointers and arrays

Array - a contiguous space containing elements of the same type

The array size is related to the element type and the number of elements

Pointer (variable) - is a variable that places the address

The size of the pointer variable is 4(32bit)/8(64bit) byte s

The array name is indeed the first element address
There are two exceptions:

  1. Sizeof (array name) - the array name here is not the address of the first element, but represents the whole array. What is calculated here is the size, unit or byte of the whole array.
  2. &Array name - the array name here is not the address of the first element, but represents the entire array. What you get is the address of the entire array
int main() { int arr[10] = { 0 }; int sz = sizeof(arr); printf("%d\n", sz); return 0; }
int main() { int arr[10] = { 0 }; int* p = arr; int i = 0; int sz = sizeof(arr) / sizeof(arr[0]); for (i = 0;i < sz;i++) { *(p + i) = i; } for (i = 0;i < sz;i++) { printf("%d ", *(p + i)); } return 0; }

6. Secondary pointer

As we all know, pointer variables are variables, and variables have addresses. Where are the addresses of pointer variables stored?

This is the secondary pointer we want to know.

int main() { int a = 10; int* p = &a; int** pp = &p;//pp is the secondary pointer **pp = 20; printf("%d\n", a);//a = 20 return 0; }

7. Pointer array

From the name, do you think pointer array is pointer or array?

The answer is an array, an array of pointers.

Integer array - an array that holds integers is an integer array
Character array - an array of characters is a character array
Pointer array - the array where pointers are stored is the pointer array
Array of int * integer pointers
Array of char * character pointers

int main() { int arr[10]; char ch[5]; int* parr[5]; char* pc[6]; return 0; }
int main() { int a = 10; int b = 20; int c = 30; int* parr[3] = { &a,&b,&c }; for (int i = 0;i < 3;i++) { printf("%d\n", *(parr[i])); } return 0; }

The above is all about our initial C language pointer. Later, I will update the advanced version of C language pointer. I hope you can have a deeper understanding of C language pointer.

6 October 2021, 21:43 | Views: 6216

Add new comment

For adding a comment, please log in
or create account

0 comments