Pointer arrays and two-dimensional array pointer variables

Source: Internet
Author: User

Pointer Array Concepts:

An array whose element value is a pointer is an array of pointers. A pointer array is a collection of ordered sets of pointers . all elements of a pointer array must be pointer variables that have the same storage type and point to the same data type.

The general form of the pointer array description is:

Type descriptor * array name [ array length ]

Where the type descriptor is the type of the variable to which the pointer value points.

For example:

int *pa[3]

indicates that the PA is an array of pointers, which has three array elements, each of which is a pointer to an integer variable .

1 , using a pointer array to point to a two-dimensional array.

Each element in the pointer array is given the first address of each row of a two-dimensional array, so it can also be understood as pointing to a one-dimensional array.

Example code:

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

    for(i=0;i<3;i++)
        printf("%d,%d,%d\n",a[i][2-i],*a[i],*(*(a+i)+i));
    for(i=0;i<3;i++)
        printf("%d,%d,%d\n",*pa[i],p[i],*(p+i));
}
In this case, the PA is an array of pointers, and three elements point to the rows of the two-dimensional array, respectively. The specified array element is then output with a looping statement.    *pa[i] represents an i row 0 column element value, because p is the same as a[0], so p[i] represents the value of 0 rows I column, * (P+i) represents the value of the 0 row I column.  int *p[3] indicates that P is an array of pointers, and that there are three subscript variables p[0],p[1],p[2] are pointer variables. 2.pointer arrays represent a set of strings and serve as function argumentsExample code 1:#include <stdio.h>
void Main ()
{
Char *day_name (char *name[],int N);

static char *name[]={"Illegal Day",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
Char *ps;
int i;

printf ("Input Day no:\n");
scanf ("%d", &i);

if (i<0)
Exit (1);

Ps=day_name (Name,i);
printf ("Day no:%2d-->%s\n", i,ps);
}

Char *day_name (char *name[],int N)
{
Char *pp1,*pp2;
Pp1=*name;
pp2=* (Name+n);
Return ((n<1| | N&GT;7)? PP1:PP2);
}
Example code 2:#include <string.h>
#include <stdio.h>

Main ()
{
void sort (char *name[],int n);
void print (char *name[],int n);
Static char *name[]={"China", "AMERICA", "AUSTRALIA",
"FRANCE", "GERMAN"};
int n=5;
Sort (name,n);
Print (name,n);
}

void sort (char *name[],int N)
{
Char *pt;
int i,j,k;
for (i=0;i<n-1;i++)
{
K=i;
for (j=i+1;j<n;j++)
if (strcmp (Name[k],name[j]) >0)
K=j;
if (k!=i)
{
Pt=name[i];
NAME[I]=NAME[K];
name[k]=pt;
}
}
}

void print (char *name[],int N)
{
int i;
for (i=0;i<n;i++)
printf ("%s\n", Name[i]);
}
  Attention should be paid to the difference between pointer arrays and two-dimensional array pointer variables.  Both of these can be used to represent two-dimensional arrays, but their representations and meanings are completely different. Two-dimensional array pointer variable two-dimensional array pointer variable is a single variable, for example int (*p) [4] representsp is a pointer variableA pointer to a one-dimensional array that contains 4 integral elements. int (*p) [4]; Indicates that the *P has 4 elements, and each element is an integer type.The object that P refers to is an array of 4 integral elements, that is, p is a pointer to a one-dimensional arrayat this point P can only point to a one-dimensional array containing 4 elements, and the value of P is the starting address of a one-dimensional array.  P cannot point to an element in a one-dimensional array. int a[3][4];    int (*p) [4];    P=a; Since P is a 0-row element that points to a two-dimensional array,so P+i is the start address of the I line of the two-dimensional array a。 * (* (p+2) +3) is the value of a[2][3]. Example code:#include <stdio.h>
void Main ()
{
void average (float *p,int n);
void Search (float (*p) [4],int N);

Float score[3][4]={
{65,67,70,60},
{80,87,90,81},
{90,99,100,98}
};

Average (*score,12); Average of 12 students
Search (score,2); Students with a number 2 are asked to score
}

void average (float *p,int N)
{
float *p_end;
float Sum=0,aver;

p_end=p+n-1;
for (; p<=p_end; p++)
sum+= (*P);
aver=sum/n;

printf ("average=%5.2f \ n", aver);
}

void Search (float (*p) [4],int N)//p is a pointer to a one-dimensional array with 4 elements
{
int i;
printf ("The score of no.%d is: \ n", n);

for (i=0;i<4;i++)
printf ("%5.2f", * (* (p+n) +i));
printf ("\ n");
}


From for notes (Wiz)

List of attachments

    Pointer arrays and two-dimensional array pointer variables

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.