C + + pointer array, two-dimensional array

Source: Internet
Author: User

One or two-D arrays

1> initialization

int A[3][3] = {{1,0,0}, {0,1,0}, {0,0,1}}; Here is {}, not ()

2> to treat two-dimensional arrays as one-dimensional arrays

void Print_array (int * p, int row, int line)

{

for (int i=0; i<row; ++i)

{

for (int j=0; j<line; ++j)

{

printf ("%d \ n", * (p + i * row + j)); Equivalent to * (* (p+i) + j); P[I][J]

}

}

}

Call

Print_array (int *) A, 3, 3);

3> Two-dimensional arrays when two-dimensional arrays are used

void Wordsort (char p[][10], int row)
{
Char temp[10];
for (int n=0; n<row; ++n)
{
for (int m=n+1; m<row; ++m)
{

Bubble sort
if (strcmp (P[m], P[n]) < 0)
{
strcpy (temp, p[n]);
strcpy (P[n], p[m]);
strcpy (p[m], temp);
}
}
}

for (int n=0; n<row; ++n)
{
printf ("%s \ n", P[n]);

}
}

Call

Char p[5][10] = {"Hello", "World", "111", "135", "134"};

Wordsort (P, 5);

4> Row pointers (two-dimensional arrays are passed as parameters of a formal parameter)

C + + pointer array, two-dimensional array

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.