Thread: Two dimensional array

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Two dimensional array

    Hello everyone,


    I am just interested in the following sample, how compiler maps one dimensional int pointer array to two dimensional int array so perfect. E.g. p [3][3] maps to the 33th element and p [0][9] maps to the 9th element.

    How does the compiler do the internal mapping of one dimensional int pointer array (variable p) and the two dimensional int array (variable buffer)?

    Code:
    int main (int argc, char** argv)
    {
    
    	int (*p) [10];
    	int buffer [10][10];
    	int tmp;
    	int i = 0;
    	int j = 0;
    
    	// to initialize
    	for  (i = 0; i < 10; i++)
    	{
    		for (j = 0; j < 10; j++)
    		{
    			buffer [i][j] = i * 10 + j;
    		}
    	}
    
    	p = &buffer;
    
    	tmp = p [3][3]; // tmp = 33
    
    	tmp = p [0][9]; // tmp = 9
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First off, it should be p = buffer; (no & needed here).

    Second, your pointer isn't a 1-D pointer as such, it is a pointer to an ARRAY of 10 integers.
    int (*p) [10] and int *p[10] might look superficially to be similar, but they're really quite different. The parentheses are really important to figuring out what is going on.

    int (*p) [10] - p is a pointer to an array of 10 ints.
    int *p[10] - p is an array of 10 pointers to int.

    > How does the compiler do the internal mapping of one dimensional int pointer array
    > (variable p) and the two dimensional int array (variable buffer)?
    Because p is the same as your array (in essence in this context). If you were to pass buffer to a function, then it's type would be the same as p.

    For the array, the elemsPerRow is obvious from the declaration, so
    arr[row][col] performs array + row * elemsPerRow + col

    Because the pointer type also contains the 'elemsPerRow' encoded into the type itself, the compiler is also able to deduce that
    p[row][col] performs p + row * elemsPerRow + col

    Basically, the same calculation gives the same answer.

    > int buffer [10][10];
    Imagine this as int (buffer [10])[10];, where the inner part tells us that buffer is an array of 10 'somethings'. The something in this particular case is an array of 10 ints.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Salem,


    I think in my code, I can change int (*p) [10] to int* p, and leave other code unchanged and the effect of the program should be the same, right?

    Quote Originally Posted by Salem View Post
    First off, it should be p = buffer; (no & needed here).

    Second, your pointer isn't a 1-D pointer as such, it is a pointer to an ARRAY of 10 integers.
    int (*p) [10] and int *p[10] might look superficially to be similar, but they're really quite different. The parentheses are really important to figuring out what is going on.

    int (*p) [10] - p is a pointer to an array of 10 ints.
    int *p[10] - p is an array of 10 pointers to int.

    > How does the compiler do the internal mapping of one dimensional int pointer array
    > (variable p) and the two dimensional int array (variable buffer)?
    Because p is the same as your array (in essence in this context). If you were to pass buffer to a function, then it's type would be the same as p.

    For the array, the elemsPerRow is obvious from the declaration, so
    arr[row][col] performs array + row * elemsPerRow + col

    Because the pointer type also contains the 'elemsPerRow' encoded into the type itself, the compiler is also able to deduce that
    p[row][col] performs p + row * elemsPerRow + col

    Basically, the same calculation gives the same answer.

    > int buffer [10][10];
    Imagine this as int (buffer [10])[10];, where the inner part tells us that buffer is an array of 10 'somethings'. The something in this particular case is an array of 10 ints.

    regards,
    George

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I can change int (*p) [10] to int* p, and leave other code
    > unchanged and the effect of the program should be the same, right?
    No, it wouldn't work at all.
    For a start, you'd lose the two dimension subscripting of your p variable later on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM