Thread: 2-d array

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    2-d array

    i've been stuck on this problem,i hope some1 would be able to help me out..here! by explaning about how did that particular outcome come? the output is coming out to be...1002 1002 1002 1 Any help would be appreciated!
    Code:
    main()
    {
    int a[2][3][4]={
                               {
                                  1,2,3,4,
                                   5,6,7,8
                                    9,1,1,2
                               },
                                 {
                                   2,1,4,7,
                                    6,7,8,9
                                    0,0,0,0
                                 }
                           };
    printf("\n%u %u %u %d",a,*a,**a,***a);
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Your formatting is just horrid.

    Because your array is 3D, your array identifier is essentially a pointer of a pointer of a pointer. Which means in order to get the value (The first element of the array) you have to dereference it three times. Hence why the first three output yield a memory address and the last yields the value.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by galmca
    i've been stuck on this problem,i hope some1 would be able to help me out..here! by explaning about how did that particular outcome come? the output is coming out to be...1002 1002 1002 1 Any help would be appreciated!
    [/code]
    Code:
    int main(int argc, char **argv)
    {
    	int a[2][3][4]={
    		{1,2,3,4,5,6,7,8,9,1,1,2},
    		{2,1,4,7,6,7,8,9,0,0,0,0}
    	};
    
    	printf("\n%u %u %u %d", a, *a, **a, ***a);
    }
    
    // a = addressof first byte in the whole array
    // *a = addressof first byte in the whole array
    // **a = addressof first byte in the whole array
    // ***a = integer value at the first byte in the whole array

    - xeddiex

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    xeddiex reminds me of Maxwell Smart.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    isn't the *a means the value at zeroth address or the base adress,if it is then why is it taking th adress of the first byte and not the value and the same goes for **a?
    not being able to understand that...can u just give me some example by taking some integer value in this example,suppose like the first element of the first array,that is,1..maybe i'll be able to understand better then...hope u can help!

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by SlyMaelstrom
    xeddiex reminds me of Maxwell Smart.
    Hmm ...In what ways or, how do I remind you of Maxwell Smart?


    - xeddiex

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by galmca
    isn't the *a means the value at zeroth address or the base adress,if it is then why is it taking th adress of the first byte and not the value and the same goes for **a?
    not being able to understand that...can u just give me some example by taking some integer value in this example,suppose like the first element of the first array,that is,1..maybe i'll be able to understand better then...hope u can help!
    No, each time you dereference, you get the value of whatever the pointer was pointing to. If it's pointing to a pointer, you still get a memory address.
    Code:
    char val, *ptr, **ptr2; // The second is a pointer to a pointer.
    val = 3;
    ptr = &val;
    ptr2 = &ptr;  // See? Still pointing to memory addresses.
    
    printf("%d", *ptr2);    // Returns a memory address (The value of ptr)
    printf("%d", **ptr2);  // Returns 3 (The value of val)
    Now how does that pertain to your array? Consider that each dimension of the array requires a pointer to the array with one less dimension.

    Code:
    arry    = &arry[][]
    *arry   = &arry[]
    **arry  = &arry
    ***arry = arry          // The value.
    Last edited by SlyMaelstrom; 03-01-2006 at 12:43 PM.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by galmca
    isn't the *a means the value at zeroth address or the base adress,if it is then why is it taking th adress of the first byte and not the value and the same goes for **a?
    not being able to understand that...can u just give me some example by taking some integer value in this example,suppose like the first element of the first array,that is,1..maybe i'll be able to understand better then...hope u can help!
    I wrote a little summary about dealing with multidimensional arrays a couple of days ago. You could probably find some usefulness in it, which can be found here (look for the post by xeddiex).


    - xeddiex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM