[C Language] Pointer Depth Analysis (1)

Pointer, as a feature of C language, plays an indispensable role in C language. To learn C language well, pointers are an important knowledge point that must be mastered.

As C language beginners, we already know the basic concepts of pointers:
1. A pointer is a variable used to store an address, which uniquely identifies a piece of memory space.
2. The size of the pointer is 4/8 bytes (32-bit/64-bit operating system respectively).
3. The pointer has a type, and the type of the pointer determines the step size of the pointer + - integer, and the authority of the pointer dereference operation.
4. Pointers can perform operations

Of course, pointers must have more than the above content. Today we will discuss pointers in depth.

    • character pointer

Character pointer, as the name suggests, is a pointer to a character, the type is char*

The address dereferencing operation of a character pointer is the same as that of an integer pointer:

int main()
{
  char ch='w';
  char *pc=&ch;
  *pc='w';
  return 0;
}

There is another way to use:

int main()
{
  const char* pstr="hello world";  //这里的const可以省略,但是在高版本编译器下可能会报警告
  printf("%S",ptsr);
  return 0;
}

The output looks like this:

Just looking at the code, we may easily misunderstand that the string hello world is put into the character pointer pstr. However, what is stored in the character pointer pstr is essentially only the address of the first character of the string hello world. Through the first character address, you can find a whole string.

Why does the comment above say that const can be omitted?

Take a look at this interview question for a more thorough understanding:

#include <stdio.h>

int main()
{
  char str1[] = "hel1lo World";
  char str2[] = "hel1lo World";
  const char *str3 = "hello World";
  const char *str4 = "hel1o World";
  if(str1 ==str2)
    printf("str1 and str2 are same\n");
  else 
    printf("str1 and str2 are not same\n"); 
  if(str3 ==str4)
    printf("str3 and str4 are same\n" );
  else
    printf("str3 and str4 are not same\n");
  return 0;
}

The result of the operation is this:

It turns out that the string stored by the character pointer is a constant string, that is to say, str3 and str4 here point to the same constant string. C/C++ usually stores constant strings in a separate memory area. When several different pointers point to the same string, they actually point to the same block of memory. But if you use the same constant string to initialize different arrays, different memory spaces will be opened up. So here str1 and str2 are different, and str3 and str4 are the same.

It is precisely because of the property of constant strings that the correct way of writing should be const char *str3 = "hello World", but since we already know that the strings here are constant strings, the const here can be omitted for convenience and written as char *str3="hello world" is also correct.

    • array of pointers

An array of pointers is an array used to store pointers.

If you can understand concepts such as integer arrays and character arrays, pointer arrays are also easy to understand.

Consider the following examples:

int *arr[10]; //Array that can store 10 integer pointers
char *arr2[5]; //Array that can store 5 character pointers
char **arr3[7]; //Can store 7 first-level array of secondary character pointers to character pointers
    • array pointer

3.1 Definition of array pointer

As the name suggests, an array pointer is a pointer that can point to an array.

The form of the array pointer is as follows:

int (*p)[10];

Here, p is first combined with *, indicating that p is a pointer variable, and then points to an integer array with a size of 10 integers. So p is an array pointer to an array.

Since [] has a higher priority than *, () must be used to ensure that p is combined before *, otherwise it is like this

int *p[10];

This becomes an array of pointers.

What is the type of the array pointer?

If we now create an integer pointer int *p1, we can clearly know that p1 is a pointer with type int *.

Then the analogy to the array pointer, for int (*p)[10], the type of p is int (*)[10];

So you can get a simple way to judge the variable type, the remaining part after removing the variable name is the type of the variable.

3.2 Use of array pointers

Since the array pointer points to the array, the address of the array should be stored in the pointer, as follows:

#include <stdio.h>
int main()
{
  int arr[10]={1,2,3,4,5,6,7,8,9,10};
  int (*p)[10]=&arr;
  return 0;
}

This implements a simple address fetch operation. If you want to print the contents of the array through the array pointer, the code is as follows:

#include <stdio.h>
int main()
{
    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
    int(*p)[10] = &arr;
    int i = 0;
    for (i = 0; i < 10; i++)
    {
        printf("%d ", *(*p)+i);//*p表示arr,也就是数组首元素地址,*((*p)+i)即是*(arr+i)
    }
    return 0;
}

The printed results are as follows:

(End of this article)

Guess you like

Origin blog.csdn.net/fbzhl/article/details/128841135