29C Language Learning Notes (Pointer and 2-D Array)

11.29C Language Learning Notes (Pointer and 2-D Array)

Follow 11.18 Learning Notes https://blog.csdn.net/FriedReich/article/details/121402242

Pointer access to one-dimensional array

#include<stdio.h>
int main()
{
    int a[3]={1,3,5};
     printf("%p\n",a);
     printf("%p\n",a+1);
     printf("%p\n",&a);//The constant address of an array is given here, so there is no change in the results of printf line 1 and line 3.
     printf("%p\n",&a+1);//This is an array of three elements that occupies 12 bytes, so when the constant address is added one unit backwards, the pointer variable displayed is added 12 units backwards.
     printf("%p\n",*(&a));//The pointer variable corresponding to the address of the array constant is interpreted here as the cancellation of two symbols
     printf("%p\n",*(&a)+1);
    return 0;
}

Pointer access to a two-dimensional array

1. Access takes precedence by row, so the number of columns is limited.

The first access is similar to a one-dimensional array (pointer variable p)

Two-dimensional array access:

A two-dimensional array a[][] is defined

The array name a corresponds to the first address of the first line.

a Move one unit back each time, corresponding to the first address of the next line.

a[0][0]pa
a[0][1]p+1
a[1][0]p+2a+1
a[1][1]p+3
a[2][0]p+4a+2
a[2][1]p+5
#include<stdio.h>
int main()
{
    int i;
    int a[3][2]={1,2,3,4,5,6};
    for(i=0;i<6;i++)
    {
    	printf("%p\t%p\n",a+i,a+i+1);
    }
    return 0;
}

Pointer and 2-D Array

Identity expression:

Value pointing to the first element in each row:

*(a+i) = a[i] (both are the first address in line I of an array of a)

Deduction:

*(a+i) +j=a[i]+j (both are the first addresses in column J of row I of an array of a)

Tip: Row address, column address plus'*'; Column Address Reline Address with "&"

Example:

1. Access to traditional one-dimensional arrays:

#include<stdio.h>
int main()
{
	int a[3][2];
	int i;
	int *p=&a[0][0];
	for(i=0;i<3;i++)
	{
		printf("%p\t%p\n",p+i,p+i+1);
        printf("%d\t%d\n",*(p+i),*(p+i+1));//If p is replaced by a here, errors will occur in the system-recognized data types
        //Normal or negative is a single pointer variable for easy access
	}
    return 0;
}

Definition of row pointer

Type identity indicator (*pointer variable name) [reshaping constant expression];

For example: int(*p) [3]

The pointer variable p is defined and points to a one-dimensional array of length 3.

(For a two-dimensional array, the pointer variable p defined should point to the same number of columns as the two-dimensional array.)

#include<stdio.h>
int main()
{    
	int a[3][2]={1,2,3,4,5,6};
	int i,j;
	int (*p)[2];//Here the pointer variable p points to an array of length 2
	p=a;//When P and a are equal, both p+i and a+i are equal to adding I rows back
    //At this point p[i] is equivalent to a[i]
    //p[i][j] is equivalent to a[i][j]
	for(i=0;i<3;i++)
	{
		for(j=0;j<2;j++)
        {
            printf("%p\t%d\n",p[i]+j,p[i][j]);
        }
	}
    return 0;
}
#include<stdio.h>
int  main()
{
	int a[3][2] = { 1,2,3,4,5,6 };
	int(*p)[2];
	p = a;
	int i, j;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 2; j++)
		{
			printf("%p\t%d\n", *(a + i) + j, a[i][j]);
			printf("%p\t%d\n", a[i] + j, a[i][j]);
			printf("%p\t%d\n", p[i] + j, a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

Pointer Array

(Define the address before labeling the corresponding value)

Type identifier * pointer variable name [integer variable expression];

For example, int *a[5];

a[0]...a[4] are pointer variables of type int

Examples of using pointer arrays to access two-dimensional arrays

int main()
{
	int a[3][2]={1,2,3,4,5,6};
	int i,j;
	int *p[3];
	for(i=0;i<3;i++)
	{
		p[i]=a[i];
		for(j=0;j<2;j++)
		{
			printf("%p\t%d\n",p[i]+j,p[i][j]);
		}
return 0;
}

Differences between pointer arrays and row pointer definitions

Row pointer definitions such as int(*p) [3] are designed to point with pointer p to an array of three units in length.

When applied to two-dimensional arrays, the pointer p assigns the defined two-dimensional array name, that is, the pointer p controls the row and the pointer p controls the column.

Pointer arrays, such as int *a[5], are designed to define a pointer to an array type in which each element corresponds to an address.

In general, the element pair of pointer arrays defined is the header address of the desired assignment array.

Tags: C#

Posted on Wed, 01 Dec 2021 13:30:18 -0500 by kwilder