Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Following code is a part of my article : <strong>Pointers and Arrays in C C++</strong></p> <p>You can check it out @ <a href="http://pointersandarrays.blogspot.com/" rel="nofollow">http://pointersandarrays.blogspot.com/</a></p> <p><strong>Pointers and 2D Arrays</strong></p> <p>Following code snippet illustrates how to declare and access a 2D array. Underneath 2D Array lies a single dimensional array. You will get be sure of this after playing with the following piece of code. </p> <p><strong>Code Snippet #4</strong></p> <pre><code> #include&lt;iostream&rt; using namespace std; int main() { cout&lt;&lt; "Understanding Pointers and 2 D Arrays"&lt;&lt;endl; cout&lt;&lt; "----------------------\n"&lt;&lt;endl; //Declaration of a 2D Array. int tab[3][5]; //Total space required : 3*5 * sizeof(int) = 15 * sizeof(int) //Funda : Since the amount of required space is known by compiler, contiguous 15 memory cells are allocated here. //Hence the case is similar to a 1 D Array of size 15. Lets try this out! //Array initialization using array name for(int i=0; i&lt;3;i++) for(int j=0; j&lt;5;j++) tab[i][j]=i+2*j; //Print array using array name cout &lt;&lt; "\nPrint array using array name ..."&lt;&lt;endl; for(int i=0; i&lt;3;i++) { for(int j=0; j&lt;5;j++) printf("%2d ",tab[i][j] ); printf("\n"); } //Print array using a pointer. Proof of 1 D array being allocated cout &lt;&lt; "\nPrint array using a pointer. Proof of 1 D array being allocated ..." &lt;&lt; endl; int *tptr; tptr = &tab[0][0]; // pointer tptr points at first element of the array. for(int i=0; i&lt;15;i++) printf("%d ",*(tptr+i) ); tptr = &tab[0][0]; cout &lt;&lt; "\nNotice that array is printed row by row in a linear fashion."&lt;&lt;endl; return 0; } </code></pre> <p><strong>Output #4:</strong> <code></p> <pre><code>Understanding Pointers and 2D Arrays Print array using array name ... 0 2 4 6 8 1 3 5 7 9 2 4 6 8 10 Print array using a pointer. Proof of 1 D array being allocated ... 0 2 4 6 8 1 3 5 7 9 2 4 6 8 10 Notice that array is printed row by row in a linear fashion. </code></pre> <p></code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload