By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,670 Members | 1,526 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,670 IT Pros & Developers. It's quick & easy.

what I miss?

P: n/a
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?

( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )

Nov 15 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"Parahat Melayev" <pa*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?


Nope. C isn't a char, it's a pointer (to a char).

Alex
Nov 15 '05 #2

P: n/a
oh yeah that is right :) panic

tnx

Nov 15 '05 #3

P: n/a


Parahat Melayev wrote:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?
what is c ????
c is the pointer which is going to hold the address which is of the
char type.

malloc will return the address of the allocated memory.
and so c holds the address, which is int.

c = malloc(sizeof(unsigned char));

HTH
ranjeet

( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )


Nov 15 '05 #4

P: n/a


Parahat Melayev wrote:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?


Not very. I know of systems where this program
would report both sizes as zero -- a good deal stranger,
don't you think?

(Hint: What is the type of the result of `sizeof',
and what is the type expected by the "%d" conversion?)

--
Er*********@sun.com

Nov 15 '05 #5

P: n/a
thanks but problem is not at malloc.
it must be,

printf("size of *c: %d\n", sizeof(*c));

Nov 15 '05 #6

P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
Parahat Melayev <pa*****@gmail.com> wrote:
thanks but problem is not at malloc.
it must be,

printf("size of *c: %d\n", sizeof(*c));


Glad to see you've solved your own problem. Congratulations!

Nov 15 '05 #7

P: n/a
Some minor nits...

Alexei A. Frounze wrote:
"Parahat Melayev" <pa*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?


Nope. C isn't a char, it's a pointer (to a char).


No, C is a programming language (hint - case sensitivity!), and c is not a
pointer to a char, but a pointer to an unsigned char.

Usual stuff follows:

A) many people prefer a full prototype for main(), as in:
int main(void)

B) using the template p = malloc(n * sizeof *p), we could improve the malloc
to:
c = malloc(sizeof *c);
(ignoring n on this occasion, since we appear only to want one object).

C) sizeof yields a size_t, which is an unsigned integer type of unknown
size (in C90), so we will need to cast it. Unsigned longs are good
for this, so make that:

printf("size of unsigned char: %lu\n",
(unsigned long)sizeof(unsigned char));

(note that this is now required to write 1 on stdout), and

printf("size of c: %lu\n", (unsigned long)sizeof c);

Superfluous parentheses removed. Note the updated format specifiers.

Did I miss anything?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #8

P: n/a
ra***********@gmail.com wrote:

Parahat Melayev wrote:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n",
sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this,
it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?


what is c ????
c is the pointer which is going to hold the address which is of the
char type.

malloc will return the address of the allocated memory.
and so c holds the address, which is int.


The address is not int.
A pointer type is different from an int type.

--
pete
Nov 15 '05 #9

P: n/a
On 2005-07-15 09:24:30 -0500, "Parahat Melayev" <pa*****@gmail.com> said:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
The size of an unsigned char is 1.

printf("size of c: %d\n", sizeof(c));
The size of a pointer is 4.

The size of the memory handled by the pointer is 1.

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?

( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )


I suggest reading a basic C book. You must know the difference between
a variable and a pointer.

PS. Sizes of vars and ptrs may vary on different platforms.

--
Sensei <se******@tin.it>

cd /pub
more beer

Nov 15 '05 #10

P: n/a
Parahat Melayev wrote:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char)); ^^
You have no reason to think that the size_t that sizeof() yields is a
signed int, as '%d' requires. In fact, we know that it is not, since a
size_t is by definition unsigned, and will often be of a wider type than
int.
printf("size of c: %d\n", sizeof(c));
return 0;
} When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?


No. c is a pointer-to-unsigned-char. Do you, for some reason, imagine
that a pointer-to-T should have the same size as a T?
Nov 15 '05 #11

P: n/a
Parahat Melayev wrote on 15/07/05 :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?

( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )


It's fine. c is a pointer. The sizeof of a pointer can be 4 (actually,
it's implementation-dependent).

Note that sizeof returns a size_t. You should use "%zu" as a formatter.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

Nov 15 '05 #12

P: n/a
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a formatter.


You should use "%zu" only if you can depend on your runtime library to
implement it. "%zu" is a new feature in C99. Even if your compiler
supports C99 (or most of it), the runtime library may not.

A reasonably safe way to print a size_t value, compatible with both
C90 and C99, is:

printf("%lu", (unsigned long)sizeof foo);

The only case where this can fail is if sizeof foo exceeds ULONG_MAX,
which can only happen if size_t is bigger than unsigned long, which
can't happen in C99. Even then, it can only happen if foo is a huge
object, 4 gigabytes or bigger.

If you're worried about that possibility, you can use the preprocessor
to check for __STDC_VERSION__ >= 199901L.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #13

P: n/a
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a formatter.


You should use "%zu" only if you can depend on your runtime library to
implement it. "%zu" is a new feature in C99. Even if your compiler
supports C99 (or most of it), the runtime library may not.


From the OP : "gcc version 4.0.0"

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 15 '05 #14

P: n/a
Emmanuel Delahaye wrote:
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a formatter.

You should use "%zu" only if you can depend on your runtime library to
implement it. "%zu" is a new feature in C99. Even if your compiler
supports C99 (or most of it), the runtime library may not.


From the OP : "gcc version 4.0.0"


So what? The OP uses gcc on a Linux system and thus probably has an
appropriate glibc to do the trick.
I can have gcc 4.0.0 on my cygwin under windows yet the runtime
library still is upset about "%zu"...
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #15

P: n/a
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a formatter.


You should use "%zu" only if you can depend on your runtime library to
implement it. "%zu" is a new feature in C99. Even if your compiler
supports C99 (or most of it), the runtime library may not.


From the OP : "gcc version 4.0.0"


That provides no clue about whether the runtime library supports "zu".

I have gcc 4.0.0 on three different systems. On one of them, the
following program:

#include <stdio.h>
int main(void)
{
size_t s = 42;
printf("s = %zu\n", s);
return 0;
}

prints "s = 42"; on the other two, it prints "s = zu". (I'd mention
which systems they are if it were relevant.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #16

P: n/a
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a

formatter.

Wow, there's a new type specifier for size_t in *printf()? Great. I used to
use long for it and to make sure it's indeed long was casting to it.

Btw, how about pointers to functions and pointers to data? The two can be
different types (I mean not the C's idea about the types but rather the
effective size in bits of them). Are there any special specifiers for each?

Alex
Nov 15 '05 #17

P: n/a
Alexei A. Frounze wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Keith Thompson wrote on 15/07/05 :

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
[...]

>Note that sizeof returns a size_t. You should use "%zu" as a

formatter.

Wow, there's a new type specifier for size_t in *printf()? Great. I used to
use long for it and to make sure it's indeed long was casting to it.


ITYM "unsigned long"/"%lu" -- this is the best bet under C89.
Btw, how about pointers to functions and pointers to data? The two can be
different types (I mean not the C's idea about the types but rather the
effective size in bits of them). Are there any special specifiers for each?


AFAIK, no.
There are length modifiers for ptrdiff_t("t"), intmax_t("j"),
char("hh"), long long("ll") and a conversion specifier for hex
fractions/floating point representation ("a","A").

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #18

P: n/a


Alexei A. Frounze wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Keith Thompson wrote on 15/07/05 :

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
[...]

>Note that sizeof returns a size_t. You should use "%zu" as a


formatter.

Wow, there's a new type specifier for size_t in *printf()? Great. I used to
use long for it and to make sure it's indeed long was casting to it.

Btw, how about pointers to functions and pointers to data? The two can be
different types (I mean not the C's idea about the types but rather the
effective size in bits of them). Are there any special specifiers for each?


Pointers to data can be converted to `void*' and printed
with "%p", just as in C89. Pointers to functions cannot be
printed except via subterfuges like printing the values of
their constituent bytes.

--
Er*********@sun.com

Nov 15 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.