C++ sizeof

A

Alf P. Steinbach

* Old Wolf:
The classical example would be:

void
func( char array[10] )
{
std::cout << sizeof( array ) / sizeof( array[ 0 ] ) <<
std::endl ;
}

On my system, this outputs 0.

Is your system a Pentium 66? ;)

I think perhaps James was heading for bed (or perhaps already in bed,
late in the evening) when he wrote the article you responded to here.
For he's well aware that sizeof(char) cannot be greater than 1, and
also, that arrays can be passed by reference in C++. Not exactly
typo's, nor exactly thinko's, but just drowso's, sort of.
 
O

Old Wolf

Although "a" and "c" are arrays, when we use their names as a values,
they are effectively pointers, aren't they? so "a" is the pointer
pointing to the first element of the array.

NO

"a" and "c" are arrays. They are never anything else.
In fact "a" is an array of 3 elements and "c" is an array
of 2 elements. This never changes.
since each element in "a" is a "int*", "a" should be "int**",
where have I done wrong?

You are confusing arrays with pointers.
An array of N elements of type T, has type "array[N] of T".
moreover, if we do: "*a", then it's "&x", isn't it? Thanks.

Yes, because *a means the same as a[0] , i.e. selecting
the first element of the array.

You might find this page helps to clarify the situation for you:
http://www.torek.net/torek/c/expr.html#therule
 
J

James Kanze

* Old Wolf:
The classical example would be:
void
func( char array[10] )
{
std::cout << sizeof( array ) / sizeof( array[ 0 ] ) <<
std::endl ;
}
On my system, this outputs 0.
Is your system a Pentium 66? ;)
I think perhaps James was heading for bed (or perhaps already in bed,
late in the evening) when he wrote the article you responded to here.
For he's well aware that sizeof(char) cannot be greater than 1, and
also, that arrays can be passed by reference in C++. Not exactly
typo's, nor exactly thinko's, but just drowso's, sort of.

Very much so (and it was probably in the morning, before I'd had
my second coffee).

Change char to double in the function definition, and you'll
often get 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top