C, C ++ pointer array and array pointer

Source: Internet
Author: User

// 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};/* define a two-dimensional array m and initialize */
INT (* P) [4]; // array pointer P is a pointer pointing to a one-dimensional array. Each one-dimensional array has four int elements.
Int I, J;
Int * Q [3]; // the pointer array Q is an array, the array element is a pointer, and the three int pointers
P = m; // P is a pointer that can direct 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 viewed as a row pointer.
{
Printf ("% 3d", ** P); // the first element of each row
Printf ("% 3d", * (* p + 1); // the second element of each row
Printf ("% 3d", * (* P + 2); // the third element of each row
Printf ("% 3d", * (* P + 3); // The 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, and 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 changed to * (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 ");

}

Related Article

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.