Thread: 3 dimentional array in C

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    3 dimentional array in C

    An array
    Code:
    int a[2][3][2] = {{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}} is declared
    
    printf("%d, %d, %d",a[1] - a[0],a[1][0] - a[0][0],a[1][0][0] - a[0][0][0]);
    gives me answer 3 6 1.....can you help me understand this answer?

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    1
    My English is very poor ,I hope you can understand!
    You must to think about the memory address!

    a[1] and a[1][0] are address,a[1]==a[1][0] ,but they are differetly;

    an address - an address == integer//a[1]-a[0] a[1][0]-a[0][0] I can understand ,but i don't know how to express! I 'm sorry!

    a[1][0][0]==2
    a[0][0][0]==1
    you can look for some material about the pointer!

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Well thanks a lot for your response..

    you are saying that a[1]==a[1][0]....can you please explain with example of memory address?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The last %d is for a value. The first two are pointers that hold multiple numbers, so what you're getting there are the number of integer addresses, between one and the other.

    Code:
    #include <stdio.h>
    
    int main(void) {
      int a[2][3][2] = {{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}};
      int i;
      printf("%d, %d, %d",a[1] - a[0],a[1][0] - a[0][0],a[1][0][0] - a[0][0][0]);
      printf("\n\nAddresses from a[0] to a[1]", a[0], a[1]);
      for(i=1;a[0]+i<=a[1];i++)
        printf("\n %p", a[0]+i);
    
      printf("\n\nAddresses from a[0][0] to a[1][0]", a[0], a[1]);
      for(i=1;a[0][0]+i<=a[1][0];i++)
        printf("\n %p", a[0][0]+i);
    
      printf("\n\n\t\t\t    press enter when ready");
      (void) getchar();
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. passing 3 dimentional array into function
    By 182blink in forum C Programming
    Replies: 3
    Last Post: 10-28-2006, 05:20 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM