Thread: About Arrays

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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, 04:07 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11: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