Output of two-dimensional arrays--(pointer output and pointer array output)

Source: Internet
Author: User

When I first saw the two names of the array pointer and array of pointers, I thought it was a thing, and when I saw the English explanation, I knew the two were different.

Pointer array: array of pointers, which is used to store pointers to arrays, that is, array elements are pointers

Array pointer: A pointer to an array, pointer to array, array can be of any dimension

The following examples illustrate:
int A[3][4]---> This needless to say, is a two-dimensional array.

int (*p) [4]---> is equivalent to int p[][4], which is a pointer to a two-dimensional array that can point to a two-dimensional array with a second dimension of 4.

Directly on the code:

  1, two-dimensional array of pointer output:

1#include <iostream>2#include <algorithm>3#include <cstdio>4 using namespacestd;5 intMain () {6     inta[3][4] = {0,1,2,3,4,5,6,7,8,9,Ten, One};7     int*p, I, J;8p = &a[0][0];//or P = a[0]; //or P = * (a+0);//or P = *a;9      for(inti =0; I <3; ++i) {Ten          for(intj =0; J <4; ++j) { Oneprintf ("%4d", *p++); A         } -printf ("\ n"); -     } the     return 0;  -}

  2, two-dimensional array of pointer array output:

1#include <iostream>2#include <algorithm>3#include <cstdio>4 using namespacestd;5 intMain () {6     inta[3][4] = {0,1,2,3,4,5,6,7,8,9,Ten, One};7     int(*p) [4], I, J; 8p =A;9      for(inti =0; I <3; ++i) {Ten          for(intj =0; J <4; ++j) { Oneprintf ("%4d", * (* (p + i) +j)); A         } -printf ("\ n"); -     } the     return 0;  -}

  3. Input and output a two-dimensional array of any row:

1#include <iostream>2#include <algorithm>3#include <cstdio>4 using namespacestd;5 voidInput (int*a,intMintN) {6printf ("Please enter a two-dimensional array of%d rows and%d columns: \ n", M, n);7      for(inti =0; I < m; ++i)8          for(intj =0; J < N; ++j)9scanf ("%d", A + I * n + j);//or scanf ("%d", &a[i*n+j]);Ten } One voidPrint (int*a,intMintN) { A      for(inti =0; I < m; ++i) { -          for(intj =0; J < N; ++j) -printf ("%4d", * (A + i * n + j));//or printf ("%4d", A[i * n + j]); theprintf ("\ n"); -     } - } - intMain () { +     inta[2][4], b[3][3];//two arrays can be set to the maximum value, the input size is considered -Input (*a,2,4);//or input (A, 2, 4);//or input (a[0], 2, 4); //or input (a[0][0], 2, 4)//////input (A, 2, 4) No, OH; +Input (*b,3,3); Aprintf"Array a:\n"); atPrint (a[0],2,4); -printf ("Array b:\n"); -Print (b[0],3,3); -     return 0; - } -  in /* - Please enter a two-dimensional array of 2 rows and 4 columns: to 1 2 3 4 + 5 6 7 8 - Please enter a two-dimensional array of 3 rows and 3 columns: the 1 2 3 * 4 5 6 $ 7 8 9Panax Notoginseng Array A: - 1 2 3 4 the 5 6 7 8 + Array B: A 1 2 3 the 4 5 6 + 7 8 9 - */

  Almost, again meet worth record again Add.

Output of two-dimensional arrays--(pointer output and pointer array output)

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.