C,c++ pointer arrays and array pointers

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/kaiming2008/article/details/5617155

Note how pointer arrays and array pointers point to two-dimensional arrays, respectively.
#include <stdio.h>
Main ()
{
static int m[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};/* defines a two-dimensional array m and initializes the */
int (*p) [4];//array pointer p is a pointer to a one-dimensional array with 4 int elements per one-dimensional array
int i,j;
int *q[3];//pointer array q is an array, array element is pointer, 3 int pointer
P=m; P is a pointer that can point directly to a two-dimensional array
printf ("--array pointer output element--/n");
for (i=0;i<3;i++)/* Outputs the values of each element in a two-dimensional array */
{
for (j=0;j<4;j++)
{
printf ("%3d", * (* (p+i) +j));
}
printf ("/n");
}
printf ("/n");
for (i=0;i<3;i++,p++)//p can be seen as a row pointer
{
printf ("%3d", **p);//The first element of each line
printf ("%3d", * (*p+1));//The second element of each row
printf ("%3d", * (*p+2));//The third element of each row
printf ("%3d", * (*p+3));//Fourth element of each row
printf ("/n");
}
printf ("/n");
printf ("--pointer array output element--/n");
for (i=0;i<3;i++)
Q[i]=m[i];//q is an array, element Q[i] is a pointer
for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
{
printf ("%3d", Q[i][j]);//q[i][j] can be replaced by * (Q[I]+J)
}
printf ("/n");
}
printf ("/n");
Q[0]=M[0];
for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
{
printf ("%3d", * (Q[0]+j+4*i));
}
printf ("/n");
}
printf ("/n");

}

C,c++ pointer arrays and array pointers

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.