what I miss?

P

Parahat Melayev

#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) )
 
A

Alexei A. Frounze

Parahat Melayev 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));
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
 
E

Eric Sosman

Parahat 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));
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?)
 
R

ranjeet.gupta

Parahat 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));
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
 
P

Parahat Melayev

thanks but problem is not at malloc.
it must be,

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

Kenny McCormack

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!
 
R

Richard Heathfield

Some minor nits...
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?
 
P

pete

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.
 
S

Sensei

#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.
 
M

Martin Ambuhl

Parahat 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));
^^
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?
 
E

Emmanuel Delahaye

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?"
 
K

Keith Thompson

Emmanuel Delahaye said:
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.
 
E

Emmanuel Delahaye

Keith Thompson wrote on 15/07/05 :
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++
 
M

Michael Mair

Emmanuel said:
Keith Thompson wrote on 15/07/05 :


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
 
K

Keith Thompson

Emmanuel Delahaye said:
Keith Thompson wrote on 15/07/05 :

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.)
 
A

Alexei A. Frounze

Keith Thompson said:
Emmanuel Delahaye said:
Keith Thompson wrote on 15/07/05 :
[...]
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
 
M

Michael Mair

Alexei said:
Emmanuel Delahaye said:
Keith Thompson wrote on 15/07/05 :

[...]

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

Eric Sosman

Alexei said:
Emmanuel Delahaye said:
Keith Thompson wrote on 15/07/05 :

[...]

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.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top