Thread: pointer/array question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    pointer/array question

    hi i'm trying to understand the relation between pointers and arrays but i have a problem that i can't figure out myself.

    i read that the name of an array in c is the adress of the first element of the array , but if that's true , i don't understand why this simple code doesn't work.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int array[4][3] = { {1,2,3} , {4,5,6} , {7,8,9} , {10,11,12} };
    
    	printf("%i\n",array);
    	printf("%i\n",&array[0][0]);
    
    	printf("%i\n",*array);
    	printf("%i\n",*(&array[0][0]));
    
    	return 0;
    }
    the problem is that when i try to dereference "array" the computer prints the adress of the first element instead of "1" whereas dereferencing "&array[0][0]" works perfect.
    can somebody please explain to me what mistake i am making?
    Last edited by saller; 01-10-2006 at 07:33 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    array: [xxx][xxx][xxx][xxx]

    You'll note that the start of the first cell is the same memory address as the start of the entire thing. What results did you expect instead of the ones that you got?

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    My knowledge of C isn't great but I am guessing the problem is that because you have declared a 2d array, deferencing it gives you the beginning of the first inner array so to speak. Adding an extra * produces the first value within that array.
    silk.odyssey

  4. #4
    Registered User freespace's Avatar
    Join Date
    Nov 2005
    Posts
    7

    Clear as mud

    Multidimensional arrays are implemented as arrays of pointers. What you have there is an array of size 4 of type int*. Each of int* points to an array of size 3 type int. *array then is the content of the first element of the pointer array, which is the address to the first element of the array it points to - this would be &array[0][0].

    *(&array[0][0]) is a similar beast to *array. array[0][0] is equal to: *(*(array+0)+0), that is the content of the first element in the int* array dereferenced, which gives you the content of the first element in the array pointed to by that pointer. So expanding it out, you have
    Code:
    *(&(*(*(array+0)+0)))
    the &* cancels out, so you are left with:

    Code:
    *(*(array+0)+0)
    which is '1'.

    I know that is clear as mud... but there it is.

    Hope it helps,
    Steve
    Last edited by freespace; 01-10-2006 at 08:58 PM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by freespace
    Multidimensional arrays are implemented as arrays of pointers. What you have there is an array of size 4 of type int*.
    Nope. Let's not start confusing things like that.
    http://67.40.109.61/torek/c/pa.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User freespace's Avatar
    Join Date
    Nov 2005
    Posts
    7
    Fair enough, I worded it badly.

    Arrays of T in value context becomes pointer to T. So a multidimension array of T becomes in value context becomes pointer of a pointer to T. In our case, array is type int**, so *array gives you *int, and it is the address of array[0][0].

    Thanks Dave

    (hope I got it right this time)

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here array in value context has type int (*)[3].
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    2
    thanks for the answers , now it works!

  9. #9
    Registered User freespace's Avatar
    Join Date
    Nov 2005
    Posts
    7
    So it is an int * array of size 3, which is what I thought (and posted). I went wrong with the "implemented as...." part... right? :P

    Cheers,
    Steve

    (got it wrong last time, lets hope third time lucky)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM