** vs 2-d array

Discussion in 'C Programming' started by Arijit Mukherjee, Aug 4, 2003.

  1. All,
    This piece of code works:

    #include <stdio.h>

    int main()
    {
    int abc[2][3] = {{1,2,3},{3,4,5}};
    printf("The value of abc[2,2] %d\n", *((*abc) + 1));
    }


    While, this:

    #include <stdio.h>

    int main()
    {
    int abc[2][3] = {{1,2,3},{3,4,5}};
    int **p = (int **)abc;
    printf("The value of abc[2,2] %d\n", *((*p) + 1));
    }

    dumps core at printf.


    Any reason why?

    Thanks in anticipation,

    Regards,
    Arijit
     
    Arijit Mukherjee, Aug 4, 2003
    #1
    1. Advertising

  2. All,
    BTW, I did not modify the printf statement - to say abc[1,2], please
    ignore the index printed. Also, I used gcc version 3.0.4 for
    compilation,

    Thanks and regards,
    Arijit
     
    Arijit Mukherjee, Aug 4, 2003
    #2
    1. Advertising

  3. Arijit Mukherjee

    Malcolm Guest

    "Arijit Mukherjee" <> wrote in message
    >
    > BTW, I did not modify the printf statement - to say abc[1,2], please
    > ignore the index printed. Also, I used gcc version 3.0.4 for
    > compilation,
    >

    Firstly, C uses the syntax array[y][x] for a 2d array. Surprisingly,
    array[y,x] is not an error. This is because of a little-used feature called
    the comma operator, which is effectively a nop operator, and because
    array[y] is an array, therefore a value, in its own right.

    The important thing you need to know is that multi-dimensional array syntax
    in C gets pretty confusing once you try to take pointers to arrays.

    Further confusion is that an array of pointers looks like a 2d array, but
    isn't the same thing at all.

    int **twod means we have a pointer to a pointer. Often a pointer points not
    to one value but to the first in a (1d) array of values, twod[0] and twod[1]
    may be completely different pointers, widely separated in memory.
     
    Malcolm, Aug 4, 2003
    #3
  4. Arijit Mukherjee

    Zoran Cutura Guest

    Arijit Mukherjee <> wrote:
    > All,
    > This piece of code works:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int abc[2][3] = {{1,2,3},{3,4,5}};
    > printf("The value of abc[2,2] %d\n", *((*abc) + 1));
    > }
    >
    >
    > While, this:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int abc[2][3] = {{1,2,3},{3,4,5}};
    > int **p = (int **)abc;
    > printf("The value of abc[2,2] %d\n", *((*p) + 1));
    > }
    >
    > dumps core at printf.
    >
    >
    > Any reason why?


    p is not appropriatly typed and initialized.

    If p is meant to stay a single pointer you need to tell the compiler
    the first dimension of the two-dimensional array it is meant to point
    to, so that when you apply pointer arithmetic the correct numbers are
    used.

    int (*p)[3] = abc;

    should do the job.

    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
     
    Zoran Cutura, Aug 4, 2003
    #4
  5. Malcolm wrote:
    >
    > "Arijit Mukherjee" <> wrote in message
    > >
    > > BTW, I did not modify the printf statement - to say abc[1,2], please
    > > ignore the index printed. Also, I used gcc version 3.0.4 for
    > > compilation,
    > >

    > Firstly, C uses the syntax array[y][x] for a 2d array. Surprisingly,
    > array[y,x] is not an error. This is because of a little-used feature called
    > the comma operator, which is effectively a nop operator, and because
    > array[y] is an array, therefore a value, in its own right.
    >
    > The important thing you need to know is that multi-dimensional array syntax
    > in C gets pretty confusing once you try to take pointers to arrays.
    >
    > Further confusion is that an array of pointers looks like a 2d array, but
    > isn't the same thing at all.
    >
    > int **twod means we have a pointer to a pointer. Often a pointer points not
    > to one value but to the first in a (1d) array of values, twod[0] and twod[1]
    > may be completely different pointers, widely separated in memory.


    I was trying to explain this the same way - arrays are contiguous block
    - while a ** would mean that the first is pointing to an array of
    pointers - and the location of the second level arrays are totally
    separated. However, what I could not understand is that the code core-d
    at the access part - not typecasting part.

    Also, if the access to the arrays were like abc[j] = abc + i*jmax + j
    - then, that expression access should have worked - which did not. The
    only way (other than abc[j]), I could access was to use the pointer
    arithmetic - and assume that abc was a **.

    That's why I am really confused!!!!

    Thanks and regards,
    Arijit
     
    Arijit Mukherjee, Aug 4, 2003
    #5
  6. Arijit Mukherjee

    Alejo Guest

    "Arijit Mukherjee" <> wrote in message
    news:...
    > All,
    > This piece of code works:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int abc[2][3] = {{1,2,3},{3,4,5}};
    > printf("The value of abc[2,2] %d\n", *((*abc) + 1));
    > }
    >
    >
    > While, this:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int abc[2][3] = {{1,2,3},{3,4,5}};
    > int **p = (int **)abc;
    > printf("The value of abc[2,2] %d\n", *((*p) + 1));
    > }
    >
    > dumps core at printf.
    >
    >
    > Any reason why?
    >
    > Thanks in anticipation,
    >
    > Regards,
    > Arijit



    1) The name of an array is pointer to the first element of the array.
    2) A 2D array is an array of arrays.

    Taking those tow points into account let's see an example:

    Consider the folollowing declaration:

    int A[3][5];

    We have declared an array of 3 elements, where each element is an array of 5
    integers. So the name of the array, A, is a pointer to an array of 5
    integers.

    int **ptr;

    ptr is a pointer to a pointer to an integer.

    ptr = A;

    is wrong because A is not a pointer to a pointer to an integer.

    int (*ptr_A)[5];

    The declaration above declares a pointer, ptr_A, to an array of 5 integers.
    So...

    ptr_A = A;

    is a correct initialization of the pointer;

    Hope this helped :)

    --

    Alejo
     
    Alejo, Aug 4, 2003
    #6
  7. Arijit Mukherjee

    Kevin Easton Guest

    Arijit Mukherjee <> wrote:
    > All,
    > This piece of code works:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int abc[2][3] = {{1,2,3},{3,4,5}};
    > printf("The value of abc[2,2] %d\n", *((*abc) + 1));
    > }
    >
    >
    > While, this:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int abc[2][3] = {{1,2,3},{3,4,5}};
    > int **p = (int **)abc;
    > printf("The value of abc[2,2] %d\n", *((*p) + 1));
    > }
    >
    > dumps core at printf.


    In the expression:

    int **p = (int **)abc;

    The `abc' is evaluated as a pointer to the first element of the array -
    a pointer to { 1, 2, 3} which is of type (int (*)[3]). You then cast
    this to (int **) and store it in p.

    When you evaluate *p, the start of the array { 1, 2, 3 } is loaded as if
    it were a pointer value (int *) - but it's not. It's an array. Then
    you do some pointer arithmetic on it, and try to dereference the result
    - which is a pointer that makes no sense at all.

    Having an int ** only makes sense if there's a real object of type "int
    *" for it to point to. The correct solution is to use:

    int (*p)[3] = abc;

    - Kevin.
     
    Kevin Easton, Aug 5, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    1
    Views:
    1,072
    Kevin Collins
    Aug 14, 2003
  2. S300
    Replies:
    4
    Views:
    22,122
    Roedy Green
    Aug 19, 2003
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    521
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    234
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    524
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page