sizeof

M

Michael DeWulf

Why does the code in example 1 output 100 and the code in example 2 output
4? Rather, I understand example 2's output, but why doesn't example 1
also output 4?

-Mike

----Example 1----
#include <iostream>

using namespace std;

int main()
{
char c[100];
cout << sizeof(c) << endl;
return 0;
}

----Example 2----
#include <iostream>

using namespace std;

int main()
{
char * c = "what's up?";
cout << sizeof(c) << endl;
return 0;
}
 
F

Frederick Gotham

Michael DeWulf posted:
int main()
{
char c[100];
cout << sizeof(c) << endl;
return 0;
}


This should print 100 on every implementation, as sizeof(char) is exactly 1
on every implementation. Therefore, sizeof( char[100] ) should be equal to
100 * 1.

#include <iostream>

using namespace std;

int main()
{
char * c = "what's up?";
cout << sizeof(c) << endl;
return 0;
}


In your former example, "c" is an array". In _this_ example, "c" is a
pointer. If the bell is ringing in your head yet, I suggest you read a good
book which explains the nature of arrays and pointers in C/C++.
 
J

Jim Langston

Michael DeWulf said:
Why does the code in example 1 output 100 and the code in example 2 output
4? Rather, I understand example 2's output, but why doesn't example 1
also output 4?

-Mike

----Example 1----
#include <iostream>

using namespace std;

int main()
{
char c[100];
cout << sizeof(c) << endl;
return 0;
}

c is an array with 100 elements.
----Example 2----
#include <iostream>

using namespace std;

int main()
{
char * c = "what's up?";
cout << sizeof(c) << endl;
return 0;
}

c is a char pointer that takes 4 bytes of memory.
 
M

Martin =?iso-8859-1?Q?J=F8rgensen?=

Jim Langston said:
c is a char pointer that takes 4 bytes of memory.

And it always does so, independent of what *it points to* (in this example
an arbitrary char-array). So if the char-array was bigger or smaller,
the pointer would still take 4 bytes of memory...


Best regards
Martin Jørgensen
 
P

Pete Becker

Michael said:
Why does the code in example 1 output 100 and the code in example 2 output
4? Rather, I understand example 2's output, but why doesn't example 1
also output 4?

char c[100];

c is an array of 100 char, so its size is 100. This is one of a couple
contexts where the name of an array does not decay to a pointer.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
M

Michael DeWulf

On Sat, 16 Sep 2006, Pete Becker wrote:

Ok, thanks. That is what I was looking for. I was thinking it would
decay to a pointer.
Michael said:
Why does the code in example 1 output 100 and the code in example 2 output
4? Rather, I understand example 2's output, but why doesn't example 1 also
output 4?

char c[100];

c is an array of 100 char, so its size is 100. This is one of a couple
contexts where the name of an array does not decay to a pointer.
 
D

Default User

Ok, thanks. That is what I was looking for. I was thinking it would
decay to a pointer.

Please don't top-post. Message rearranged. See the FAQ for more details.


The major time the name of an array name is not converted in when used
with the address-of operator (&). So if you did &c, it would not be a
pointer to pointer to char, but a pointer to array 100 of char.



Brian (WUSTL grad)
 
R

Ron Natalie

Martin said:
And it always does so, independent of what *it points to* (in this example
an arbitrary char-array). So if the char-array was bigger or smaller,
the pointer would still take 4 bytes of memory...
Furthermore, it always points to a single char. If that single
char might be followed by others is no concern of the pointer.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top