Thread: Confused about certain Array statements

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    New Delhi
    Posts
    5

    Confused about certain Array statements

    Hi,

    I am a little confused about certain commands related to pointers & arrays.

    Code:
    arr[5] = {1,2,3,4,5};
    
    printf ("%u %u), arr+1,&arr+1);
    Though I understand the what the output would be however, i dont understand the logic behind the output of &arr+1 because when we specfiy an array by its name or with "&" ampersand character we are refering to the first element of the array. But the second statement refers to the whole array as one element.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But the second statement refers to the whole array as one element.
    Yes, that is correct.

    arr is the equivalent of &arr[0]

    But &arr is a completely different thing - it points to the WHOLE array.

    As a type, &arr (for a single dimension array) has the same type as a pointer to a 2D array with the same sized minor dimension.

    Consider
    Code:
    int a[5];
    int b[10][5];
    
    int (*pa)[5];
    
    pa = &a;
    pa = b;  // b[0][0]
    pa++;   // b[1][0]
    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.

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. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. confused with array
    By cman in forum C Programming
    Replies: 5
    Last Post: 02-15-2003, 08:52 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM