pointers and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dasarisrikar
    New Member
    • Sep 2006
    • 18

    pointers and arrays

    can anybody explain me the concept of multi dimensional arrays that deals with pointers in detail....

    i hav declared a 3 dimesional array like a[2][3][2]={{{2,4},{7,8}, {3,4}},{{2,2},{ 2,3},{3,4}}};

    then
    a,*a,**a,***a,a +1,*a+1,**a+1,* **a+1...etc what it means?

    can any body explain me how to represent arrays in pointer notation for 3 dimensional arrays
    ( like in one dimensional arrays &a[1] can be writen as (a+1) and a[1] as *(a+1)....)
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Taking this, slightly modified example

    int a[2][3][2]={{{2,4},{7,8}, {3,4}},{{2,2},{ 9,3},{3,4}}};

    Declares a 3 dimensional array of int that contains a total of 12 integers 2 x 3 x 2

    a has type int (*)[3][2]
    a[0] has type int (*)[2]
    a[0][0] has type int *

    taking

    int *p = &a[0][0][0];

    and treating p as an int array you should find that

    p[0] == 2
    p[1] == 4
    p[2] == 7
    p[3] == 8
    ...
    p[8] == 9
    ...
    p[11] == 4

    notice that a[1][1][0] is equivilent to p[8]

    8 = (((1 * 3) + 1) * 2) + 0

    this may not be clear but each step is take the array index used and multiply by the size of the next index. Noteated in algabra as

    Declarations

    int a[SX][SY][SZ];
    int *p = &a[0][0][0];

    then

    a[X][Y][Z] is equivilent to p[ (((X * SY) + Y) * SZ) + Z ]

    Comment

    • dasarisrikar
      New Member
      • Sep 2006
      • 18

      #3
      Hello friend,
      I am a basic learner....i dint understand the words like
      .. a has type int (*)[3][2]
      a[0] has type int (*)[2]
      a[0][0] has type int *
      for the above question......
      actually what int (*) syntax mean...pls explain the above three statements meanings deeply........

      Comment

      • dasarisrikar
        New Member
        • Sep 2006
        • 18

        #4
        i was asked this proble in an exam.....

        main()
        {
        int a[2][3][2]={{{2,4},{7,8}, {3,4}},{{2,2},{ 9,3},{3,4}}};
        printf("%u %u %u %d\n",a,*a,**a, ***a);
        printf("%u %u %u %d\n",a+1,*a+1, **a+1,***a+1);
        }

        could u explain the solution for above problem......

        Thanks & Regards

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          OK well lates take a step back given these declareations

          int x;
          int *y;
          int **z;

          then

          x has type int, i.e. the expresion x evaluates to value of type int
          y has type int * i.e. the expresion y evaluates to value of type int *, a pointer to an integer
          z has type int ** i.e. the expresion z evaluates to value of type int **, a pointer to a pointer to an integer

          YOU NEED TO UNDERSTAND THIS PARAGRAPH
          Now the type "int (*)[2]" can be read as a pointer to an integer array of size 2, "int (*)[3][2]" is a pointer to a 2 dimensioned integer array with dimension sizes 3 and 2.

          So what is the value of a in the question?
          well a is an array int a[2][3][2] so the expression a returns the value &a[0] which has type int (*)[3][2], this is a pointer to the first item in the array a[2] (&a[0]).

          So what is the value of *a in the question?
          well a has type "int (*)[3][2]" from above so *a has type int [3][2], since this is an array it is converted to a pointer by the compiler as &(*a)[0] type "int (*)[2]" which is quite complex but basically it becomes a pointer to the first item in the array a[2][3] (&a[0][0]),

          What is the value of **a in the question?
          well *a has type "int (*)[2]" from above so **a has type int [2], since this is an array it is converted to a pointer by the compiler as &(**a)[0] type int * which is quite complex but basically it becomes a pointer to the first item in the array a[2][3][2] (&a[0][0][0]),

          Note that it is always converted to a pointer to the first entry in the array so ignoring type

          a == *a == **a

          Finally what is the value of ***a
          well **a has type "int *" from above so ***a has type int, this is not an array it is an int value reference by pointer through it's array name. This will be the first entry in the array a[2][3][2], a[0][0][0].


          Now on to a+1
          now a has type "int (*)[3][2]" from above, so a+1 is this pointer moved on by 1. In C pointer arithmatic garuntees that if you add 1 to a pointer it moves on by the size of the thing pointed to. That is for any type T (int, float, double, struct etc.)

          T *pT;

          (pT+1) - pT = sizeof(T) = sizeof(*pT)

          so a+1 is a moved on by the sizeof(*a) a has type "int (*)[3][2]" therefore sizeof(*a) == sizeof(int *(*)[3][2]) == sizeof(int [3][2]) or the size of a 2 dimensioned array of integers dimension 3 and 2. So where a = &a[0], a+1 = &a[1].

          Similarly for *a+1
          *a has type "int (*)[2]" so *a+1 is a moved on by sizeof(int [2]), the size of what *a points to. Where *a == &a[0][0], *a+1 == &a[0][1]

          And **a+1
          **a has type "int *" so **a+1 is a moved on by sizeof(int), the size of what **a points to. Where **a == &a[0][0][0], **a+1 == &a[0][0][1]

          But ***a+1
          ***a has type int, suddenly we are no longer doing pointer arithmatic we are doing integer arithmatic, take the value of the integer ***a and add 1. Since ***a == a[0][0][0] == 2 then ***a+1 == 2+1 == 3

          Comment

          • dasarisrikar
            New Member
            • Sep 2006
            • 18

            #6
            Thankyou BANFA....thanks for your reply.....i am getting new things and i got one more doubt..pls clarify it......

            you said....

            So what is the value of a in the question?
            well a is an array int a[2][3][2] so the expression a returns the value &a[0] which has type int (*)[3][2], this is a pointer to the first item in the array a[2] (&a[0]).

            my doubt is....
            How the type of a be int (*)[3][2]..and *a of int (*)[2]...so on.
            How this is a pointer to the first item in the array a[2] and how a[2] is related to (&a[0])
            could you tell me the detailed explanation of the above wt u said.

            Thankyou & Regards.

            Comment

            • dasarisrikar
              New Member
              • Sep 2006
              • 18

              #7
              waiting for your reply.....

              Comment

              • dtimes6
                New Member
                • Oct 2006
                • 73

                #8
                have you ever seen code like this:
                Code:
                int a[20];
                5[a] = 5;
                printf("%d",a[5]);
                in fact a is address(pointer ) to the array, a[5] is just *(a + 5), and this tells 5[a] is just *(5 + a). The two are same in result, but they are different: in a[b] , a is the base address, b is the address to the base address.

                Comment

                Working...