pointer to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destination
    New Member
    • Dec 2009
    • 34

    pointer to an array

    In general int (*p)[3] means p is a pointer to an array which stores 3 integers.
    Correct me here if i am wrong.

    look at this code
    int (*p)[3]={1,2,3};
    Is the above statement valid?If not then why.pls explain it.

    Also what will int (*p)[3] hold? i mean an int pointer or an int
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    There's a subtle difference.
    An array name is a pointer, example: int arr[5] -- arr is a pointer to an array of 5 ints.
    int *p[5] is an array of 5 pointers to integers - not a pointer to an array of intergers

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Code:
      int *p1[5];  // An array of 5 pointers to integers
      int (*p2)[5];// A pointer to an array of five integers.
      int arr[5]; // An array of 5 integers
      arr - a pointer to the first entry in the array
      &arr - a pointer to the array
      
      int (*p)[3]={1,2,3}; // Wont compile, it attempts to initialise a pointer to an aray of 3 integers
      Also what will int (*p)[3] hold? i mean an int pointer or an int
      Neither you said it yourself in your first sentence
      int (*p)[3] means p is a pointer to an array which stores 3 integers

      Comment

      • destination
        New Member
        • Dec 2009
        • 34

        #4
        actually i interpreted int(*p)[5] wrongly.that was the reason for that confusion.
        No confusions now .Thanx.

        Comment

        Working...