pointers to array initialization

This is a discussion on pointers to array initialization within the C Programming forums, part of the General Programming Boards category; In my class i found assigning an array of 4 integer elements to a pointer in the below fashion...i did ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43

    pointers to array initialization

    In my class i found assigning an array of 4 integer elements to a pointer in the below fashion...i did not get how is this possible
    Code:
    int a[4];
    int (*p)[4]=&a;
    it should be some thing like
    Code:
    int *p=a;
    "&a" is nothing but "a" itself right which stores the base address of the array. Then whats the meaning "int (*p)[4] = &a".....can it be "int (*p)[4]=a" will bothe give the same result

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    &a is the address of a, which is a pointer. Therefore &a is a pointer to a pointer, which is what int (*p)[4] declares.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Last edited by Dave_Sinkula; 10-24-2006 at 09:42 PM. Reason: ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    That means u say that the below declaration is perfectly right ?
    Code:
    int a[4];
    int (*p)[4]=&a;
    but whats the significance of 4 there...if we give 7 there will this program work..?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by enggabhinandan
    That means u say that the below declaration is perfectly right ?
    Code:
    int a[4];
    int (*p)[4]=&a;
    but whats the significance of 4 there...if we give 7 there will this program work..?
    I imagine you tried that and your compiler at least gave you a warning.

    A pointer to an array of 4 ints should not be happy with initialization to an array of 7 ints.

    Arrays have a size. So a pointer to an array must know the size.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 06:13 AM

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