About Arrays

This is a discussion on About Arrays within the C Programming forums, part of the General Programming Boards category; Hello guys, I have a problem which need your help..... There is an array having 10 integers. Integers occupy 4 ...

  1. #1
    Banned
    Join Date
    Jul 2011
    Posts
    12

    About Arrays

    Hello guys, I have a problem which need your help.....
    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    a+1 is a pointer (a is converted to pointer, and 1 added, so the result is a pointer). On a 32 bit system, the size of a typical pointer is 4.

    A common lie told is that arrays and pointers are equivalent. They are actually equivalent in some circumstances but not in others (in the sense that the name of an array can be implicitly converted into a pointer). Your example is one of the circumstances where they are not equivalent.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Perhaps you were wanting sizeof(a) + 1, which will give you 41...?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sizeof will only work on an array declared in the same scope. If you are passing the array to a function, and trying to use sizeof there, all you will have is the size of the pointer to that type.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  3. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 10:14 PM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM

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