Lecture%2006%20-%20%20Pointer%20to%20a%20pointer

Lecture%2006%20-%20%20Pointer%20to%20a%20pointer - Lecture...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Copyright @ 2009 Ananda Gunawardena Lecture 06 2D Arrays & pointer to a pointer(**) In this lecture More about 2D arrays How a 2D array is stored Accessing a 2D array using pointers ** or pointer to a pointer Passing pointer to a function Further readings Exercises More about 2D arrays An array is a contiguous block of memory. A 2D array of size m by n is defined as int A[m][n]; The number of bytes necessary to hold A is m*n*sizeof(int). The elements of A can be accessed using A[i][j] where i can be thought of as the row index and j can be thought of as the column index. Now we take a look at how 2D arrays are store their elements. For example, #define n 2 #define m 3 int A[n][m]; OR can be defined and initialized as int A[2][3]={{1,2,3},{4,5,6}};
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyright @ 2009 Ananda Gunawardena How a 2D array is stored A 2D array is stored in the memory as follows. Entries in row 0 are stored first followed by row 1 and so on. Here n represent the number of rows and m represents the number of columns. 2-D arrays are represented as a contiguous block of n blocks each with size m (i.e. can hold m integers(or any data type) in each block). The entries are stored in the memory as shown above. Recall that when a 1D array is declared as int A[n]; the name of the array A, is viewed as a pointer (const) to the block of memory where the array A is stored. In other words, A, A+1, A+2, etc represent the addresses of A[0], A[1], A[2] etc. . We can access array elements using [ ] operator as A[i] or using pointer operator *(A+i). In fact, [ ] operator must exactly perform the operations as follows. Find the address of the i
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

Lecture%2006%20-%20%20Pointer%20to%20a%20pointer - Lecture...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online