Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > what I miss?

Reply
Thread Tools

what I miss?

 
 
Parahat Melayev
Guest
Posts: n/a
 
      07-15-2005
#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- )

 
Reply With Quote
 
 
 
 
Alexei A. Frounze
Guest
Posts: n/a
 
      07-15-2005
"Parahat Melayev" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Parahat Melayev
Guest
Posts: n/a
 
      07-15-2005
oh yeah that is right panic

tnx

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-15-2005


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?)

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      07-15-2005


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


 
Reply With Quote
 
Parahat Melayev
Guest
Posts: n/a
 
      07-15-2005
thanks but problem is not at malloc.
it must be,

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

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      07-15-2005
In article <(E-Mail Removed) .com>,
Parahat Melayev <(E-Mail Removed)> 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!

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-15-2005
Some minor nits...

Alexei A. Frounze wrote:

> "Parahat Melayev" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      07-15-2005
(E-Mail Removed) 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
 
Reply With Quote
 
Sensei
Guest
Posts: n/a
 
      07-15-2005
On 2005-07-15 09:24:30 -0500, "Parahat Melayev" <(E-Mail Removed)> 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- )


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 <(E-Mail Removed)>

cd /pub
more beer

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments