About Arrays

Discussion in 'C' started by God_Gracious, Jun 15, 2011.

  1. God_Gracious

    God_Gracious New Member

    Joined:
    Jun 13, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Respected SIR

    There is an array having 10 integers.
    Integers occupy 4 bytes.
    So when sizeof(a) is applied, result is 40 as expected,
    but when sizeof(a+1) is calculated, it gives result 4.
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    (a + 1) is treated as (int *) and sizeof(a + 1) returns 4
     
  3. kupparamakrishna

    kupparamakrishna New Member

    Joined:
    Jun 23, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    banglore
    basically when u write (a+1) it means a[1] so sizeof(a+1) is equal to sizeof(a[1]) so naturally sizeof(a[1]) is 4bytes...
    hope u got ur answer
     
  4. scorpio

    scorpio New Member

    Joined:
    Jun 21, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    no dear basically an integer occupies 2 bytes in memory and if u wrote size of(a+1) where a is an integer which occupies 2 bytes and 1 is also an integer which also occupies 2 bytes and the total memory becomes 4 bytes 2 bytes of each integer...
     
  5. scorpio

    scorpio New Member

    Joined:
    Jun 21, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    do you help me in making a program.
    that's the program qs. below
    Take 5 country names in an array (string) and perform sorting in alphabetical order.country starts with A comes 1st ano on and on
     
  6. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Just try printf("size of int : %d\n", sizeof(int)) in TurboC (freeware 2.01 originally for MS DOS) and in borland C++ freeware for windows (bcc32) -
    you will find that for
    turboC you get 2 and for
    bcc32 you get 4.

    So sizeof(int) is implementation dependent.

    And (a + 1) does not refer to a[ 1 ] --
    *(a + 1) refers to a[ 1 ]
     
  7. kupparamakrishna

    kupparamakrishna New Member

    Joined:
    Jun 23, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    banglore
    Iam sorrry if im wrong because i saw this type of notation in the book called ANSI C by BYRON S GOTTFRIED.
     
  8. kupparamakrishna

    kupparamakrishna New Member

    Joined:
    Jun 23, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    banglore
    I mean sizeof(a+1) means a[1]..
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice