C language -- pointer and one-dimensional array (high level chapter 27)

  

 

Arrays and pointers

Procedure 1

Pointer to array element

// 27-1 Pointer to array element.c

#include <stdio.h>

main()
{
    //Define a one-dimensional array
    int a[10] = {1,2,3,4,5,6,7,8,9,10};

    int *p;  //Define an integer pointer

    p = &a[0];  //Fetch array a[0]Memory address of element if: p = &a[10]  p++ System error

    *p = 20;   // 20 Assign to a[0]

    printf("%d\n",a[0]);  //First element of output array

    p++;    //Move a pointer to an array element to point to the next element

    *p = 30;

    printf("%d\n", a[1]);

}

 

       

 

  

Note:

※ the pointer + + is not the content pointed to by the pointer + +, but the pointer itself + +, which is equivalent to moving the pointer forward by one position.

※ each time the pointer + +, it does not move a byte. The number of bytes it moves is just the number of bytes occupied by the pointer type. Students can also use sizeof function to measure how many bytes are moved each time.

 

For example:

      double *pd;

      pd++;           /* PD moved 8 bytes at a time*/

 

※ generally, the addition and subtraction method is used when pointing to array elements, and multiplication is often used when pointing to two-dimensional arrays

※ pointer comparison is to compare whether two pointers point to the same position. In pointers pointing to array elements, it is often used to test whether two pointers point to the same array element.

Memory computing

Define double arr[1000]; It will occupy 8*1000=8000=8K memory. You can use sizeof(arr) to try it yourself.

Defining a pointer variable consumes 4 bytes of memory.

Pointer array

Format:

        int *a[10];        // It is equivalent to defining ten elements, all of which are pointer types

When defining variables, although each operator has different meanings, it also conforms to the operator priority of C language.

Here, a is first combined with [10], indicating that a is an array, and then combined with int *, indicating that each element of the array is of type int *, that is, an int pointer.

 

Procedure 2

Pointer array

// 27-2 Pointer array.c

#include <stdio.h>

main()
{
    int a, b;
    int *pa[10];   //Define pointer array

    pa[3] = &a;  //Take variable a Address of
    pa[6] = &b;  //Take variable b Address of

    a = 40; b = 50;
    printf("pa[3] = %d ,pa[6] = %d \n", *pa[3], *pa[6]);  //Output value
}

Pointer to array

Format:

        int  (*a)[10];

A is first combined with *, indicating that a is a pointer, and then combined with [10], indicating that it points to an array with 10 elements, and then combined with int, indicating that the array is of type int.

 

Procedure 3

Pointer to array

// 27-3 Pointer to array.c
//

#include <stdio.h>

main()
{
    int a[10] = {1,2,3,4,5};
    int(*pa)[10];   //pa Is a pointer to an array of 10 elements

    pa = &a;

    printf("%d\n", (*pa)[2]);  //Asterisk value
}

Cutting memory method:

      *a:   A is combined with * first. No matter what is on both sides, it means that a is just a "1" pointer to what it points to. We'll talk about it later.

      a[10]:   A is first combined with [10], indicating that a has "10" elements. What types of these 10 elements are? We'll talk about them later.

Array name and pointer

Procedure 4

Array name and pointer

 

// 27-4 Array name and pointer.c

#include <stdio.h>

main()
{
    int a[10] = {10,1,2,3,4,5};
    int *pa = a;  // array a Is a pointer

    //*(pa+0) = *pa Points to the first element of the array. The array name can be used directly as a pointer,*(a+0)  = *a Points to the first element of the array
    printf("%d,%d,%d\n",*(pa+0),*(a+0),*a);
    printf("%d\n", *(pa + 3));
}

 

※ the array name is actually a read-only pointer. The [n] operation has long been designed by the designer of C language. Taking the value of a[n] in the computer is equivalent to * (a+n)

Difference between * (a+n) and * a+n:

        

 

 

 

 

 

※ the difference between array name and pointer is that the array name is read-only, while the pointer can be rewritten.

Suppose a is the array name and p is the pointer, p + + is OK and a + + is not.

※ because a[0] is equivalent to * (a+0) is equivalent to * a, &a[0] has the same meaning as a. Therefore, P = & a[0] has the same effect as p=a.

Decomposition of + +

If p is a char pointer with a value, if the following expression is encountered

      char a=*p++;

It can be decomposed into:

      char a=*p;

      p=p+1;

The second case:

      char a=*++p;

Can be decomposed into

      p=p+1;

      char a=*p;

The third case:

      char a=(*p)++;

Can be decomposed into

      char a=*p;

      (*p)=(*p)+1;

The fourth case:

      char a=++(*p);

Can be decomposed into

      (*p) = (*p) +1;

      char a=(*p);

  

Chart summary

    

Output all the elements in an array with a pointer

// 27-5 Output all the elements in an array with a pointer.c. 

#include <stdio.h>

main()
{
    //Defines an ordinary integer array
    int a[6] = { 0,1,2,3,4,5 };
    
    //Pointer output to array elements
    int *pa;
    printf("\"Pointer to array element:\"\n");
    for (int i = 0; i < 6; i++)
    {
        pa = &a[i];
        printf("%d ", *pa);
    }
    printf("\n\n");

    //Pointer array output
    int *pa1[10];
    printf("\"Pointer array:\"\n");
    for (int i = 0; i < 6; i++)
    {
        pa1[i] = &a[i];
        printf("%d ", *pa1[i]);
    }
    printf("\n\n");

    //Pointer output to array
    int(*pa2)[6];
    printf("\"Pointer to array:\"\n");
    pa2 = &a;
    for (int i = 0; i < 6; i++)
    {
        printf("%d ", (*pa2)[i]);
    }
    printf("\n\n");


    //Array name and pointer output
    int *pa3 = a;
    printf("\"Array name and pointer output:\"\n");
    for (int i = 0; i < 6; i++)
    {
        printf("%d ", *(pa3++));
    }

}

 

Tags: C

Posted on Tue, 02 Nov 2021 10:03:25 -0400 by supernova