pointer to int array?

This is a discussion on pointer to int array? within the C Programming forums, part of the General Programming Boards category; Hi, I have been battling with this one for too long, so I'll just ask you guys here, lets start ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25

    pointer to int array?

    Hi, I have been battling with this one for too long, so I'll just ask you guys here, lets start slow.

    Code:
    typedef int (*ImagePointer)[3];  // is this same as if I say
    ImagePointer my_point;  // int (*my_point)[3]; (pretty sure it is)
    Now moving on

    Code:
    typedef int (*ImagePointer)[3];
    
    ImagePointer my_point;
    int a[3];
    
    my_point = &a; // this is where it starts confusing me, the problem is that I don't even //understand what exactly is a pointer to an int array
    So basically what I am asking for is a quick explanation what exactly is this pointer, because it is not an array of pointers (i.e. int *p[3], this would give me 3 pointers), or just an example, examples can teach a lot .

    Thanks

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I think this is a pointer to a 2d array of integers (3x3).

    without the brackets it would be 3 pointers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    It's just one pointer, but it points at 3 ints (in an array).

    Code:
    int a[3];
    int *pa = &a[0];  // an integer
    int (*pa)[3] = &a;  // an array of 3 ints
    You need the parentheses so you get "pointer (singular) to an array (of 3 ints)".
    Without them, int *pa[3] is "array of (three) pointers (plural) to int".

    Also, you can do this
    Code:
    int a[3];
    int b[7][3];
    int (*pa)[3];
    pa = &a;
    pa = b[0];
    pa = &b[0][0];
    You see the same syntax when you pass a 2D array as a parameter to a function.


    Consider this alternative example
    Code:
    struct foo {
      int a;
      int b;
      int c;
    } bar;
    int *pa = &bar.a;  // pointer to one int
    struct foo *pa = &bar;  // pointer to the whole thing
    In both cases, one is a pointer to the first element of the compound object (array or struct), and the second is a pointer to the whole thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    Thank you very much Salem, this is what I call an explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21