sizeof() more then just 1?

Started by
10 comments, last by Anon Mike 18 years, 7 months ago
hey, this piece of code: aChar = sizeof (char); will print a smiley and is character 1, but can i print for example character 2 with sizeof? if its possible then how?
Advertisement
Wait.... what? Why are you doing this thing?

If you want to set aChar to 1, set it to 1. There's no need to use sizeof() here. It's not at all what sizeof() is used for.
but where is sizeof() needed for anyway?
sizeof is an operator which returns the size of a datatype. You won't need it very often in C++.
With sizeof() you can determine how big something is in terms of chars.

For instance if sizeof(int) returns 4, then an int takes up the same space as 4 chars would.
Quote:Original post by jordi_0071
but where is sizeof() needed for anyway?


It's from C which uses malloc/free for dynamic memory allocation and void * for generics. C++ uses new/delete and templates, which can infer the size, for the same purposes.
Quote:Original post by nmi
With sizeof() you can determine how big something is in terms of chars.

For instance if sizeof(int) returns 4, then an int takes up the same space as 4 chars would.


sizeof(int) is equal to 4 on your compiler. sizeof(int) can be theoretically anything.
Quote:Original post by SiCrane
Quote:Original post by nmi
With sizeof() you can determine how big something is in terms of chars.

For instance if sizeof(int) returns 4, then an int takes up the same space as 4 chars would.


sizeof(int) is equal to 4 on your compiler. sizeof(int) can be theoretically anything.

Which is probably why the sentence started with 'For instance if'.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
jordi_0071, what are you trying to do? I mean in a very general sense. What is the goal of your program, or that part of your program? We can help you out, but I am 100% that the answer does not involve sizeof.

Not only do beginners often not know the answers, they don't even know what questions to ask. So when you come for help say "I'm trying to do this, I tried it this way and it doesn't work, what should I do?"
Quote:Original post by SiCrane
Quote:Original post by nmi
With sizeof() you can determine how big something is in terms of chars.

For instance if sizeof(int) returns 4, then an int takes up the same space as 4 chars would.


sizeof(int) is equal to 4 on your compiler. sizeof(int) can be theoretically anything.


That's what I said, since it was just an example.
For 16 Bit systems, size(int) may return 2 as well.

This topic is closed to new replies.

Advertisement