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.
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.
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.
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"
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.