Re: sizeof()'s strange behviour
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 08 Apr 2008 11:16:40 +0000
arnuld said:
sizeof() operator gives 2 different types of size outputs
The sizeof operator takes either an expression or a parenthesised type as
its operand. Expressions have a type; the value that sizeof yields as its
result is either the size of the parenthesised type or the size of the
type of its expression operand.
Thus, sizeof(int) and sizeof 42 both yield the size of an int, in bytes.
:\ but I do not
understand why:
#include <stdio.h>
#include <stdlib.h>
int my_size( char [] );
int main()
{
char s[] = "Saurabh Nirkhey";
s is an array of 16 char, and one char occupies one byte, so the size of s
is 16 bytes...
printf("sizeof(%s): %d\n", s, sizeof(s));
....so we'd expect this to print 16 in place of the second % format
specifier. Note that your printf is misleading. It will print:
sizeof(Saurabh Nirkhey): 16
but in fact it's sizeof s that is 16.
printf("sizeof(%s): %d\n", s, my_size(s));
See below.
<snip>
int my_size( char s[] )
{
return sizeof(s);
}
Firstly, in this context, s is a pointer to char. sizeof(s) - or indeed
sizeof s - will therefore yield the size of a pointer to char, which is
probably (but not necessarily) 4 on your system, and your sample output
confirms that it is in fact 4.
Secondly, since my_size doesn't need to modify the data pointed to by s, I
suggest that you make the parameter 'const':
int my_size(const char s[])
Thirdly, note that sizeof yields a size_t value, not an int value. A
conversion is supplied for you, but this might break for really big
objects, giving a misleading result.
Anyway, bottom line is: what you've discovered is that you can't pass
arrays to functions. You can fake it, but you can't actually pass the
array itself. If you try to take an array's value (e.g. by passing it to a
function or adding it to an integer value), what you actually get is a
pointer to the array's first element.
See K&R2, p99, which says that "in evaluating a[i], C converts it to *(a +
i) immediately", from which we can arrive at the following pointer
calculus:
a[i] is equivalent to *(a + i)
&a[i] is equivalent to &*(a + i)
& and * cancel
&a[i] is equivalent to (a + i)
set i to 0
&a[0] is equivalent to (a + 0)
&a[0] is equivalent to (a)
&a[0] is equivalent to a
So evaluating the name of an array gives a pointer to the first element,
QED.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- Follow-Ups:
- Re: sizeof()'s strange behviour
- From: arnuld
- Re: sizeof()'s strange behviour
- From: Keith Thompson
- Re: sizeof()'s strange behviour
- References:
- sizeof()'s strange behviour
- From: arnuld
- sizeof()'s strange behviour
- Prev by Date: Re: C90 long long
- Next by Date: problem with multiple source file
- Previous by thread: Re: sizeof()'s strange behviour
- Next by thread: Re: sizeof()'s strange behviour
- Index(es):
Relevant Pages
|