Different ways to cast?

E

Eric Sosman

JS said:
On this page under "Malloc":

http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

I found this:


ptr = (*int) malloc(sizeof(int)*N)


But when I try to compile it in a main method I need to write:

int *ptr;
ptr = (int *) malloc(sizeof(int));


why does the example cast like this (*int)

and when I compile I need to write:

(int *)

Because the example is wrong, R-O-N-G, wrong. Even
after fixing the typographical error (as you did), the
example demonstrates poor technique; a far better idiom is

ptr = malloc(N * sizeof *ptr);

"Why is that better?" you ask. First, the cast is
unnecessary: malloc() returns a `void*' value, and this
type converts automatically to and from any data pointer
type. Second, `sizeof *ptr' is better than `sizeof(int)'
because it is guaranteed to be the proper size. That is,
it avoids errors like

short **p = malloc(N * sizeof(short));

.... where `sizeof(short)' should have been `sizeof(short*)'.
This latter error is more insidious when multiple types
are flying around:

struct query_s { ... } *p;
struct response_s { ... } *q;
...
p = malloc(sizeof(struct query_s));
FillInQueryData(p);
q = malloc(sizeof(struct query_s)); /* oops! */
GetResultsOfQuery(p, q);
 
A

Andrey Tarasevich

JS said:
On this page under "Malloc":

http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

I found this:


ptr = (*int) malloc(sizeof(int)*N)


But when I try to compile it in a main method I need to write:

int *ptr;
ptr = (int *) malloc(sizeof(int));


why does the example cast like this (*int)

and when I compile I need to write:

(int *)
...

Firstly, it is an obvious typo in the example. It was definitely
supposed to be '(int *)'.

Secondly, in ANSI/ISO C in this context you don't need to use a cast at
all. Moreover, to apply an explicit cast to the result of 'malloc' is a
bad practice. The tutorial you are using is helplessly outdated (unless
you were specifically looking for a tutorial on K&R C).
 
A

Alan Balmer

Firstly, it is an obvious typo in the example. It was definitely
supposed to be '(int *)'.
A highly consistent typo - the same error appears throughout the
article, along with a statement that malloc needs to know "what the
type of the pointer variable is". A call which allocates memory for a
structure has the same error.

I didn't look further in the tutorial, but I'd be very wary of it.
Secondly, in ANSI/ISO C in this context you don't need to use a cast at
all. Moreover, to apply an explicit cast to the result of 'malloc' is a
bad practice. The tutorial you are using is helplessly outdated (unless
you were specifically looking for a tutorial on K&R C).

But it's marked "Last updated: 09 July 2001 09:59".
 
C

CBFalconer

Alan said:
.... snip ...

I didn't look further in the tutorial, but I'd be very wary of it.


But it's marked "Last updated: 09 July 2001 09:59".

Sounds like something of which Schildt could be proud.
 

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

Similar Threads

C pipe 1
cast question 4
cast 20
The different ways to use argv 8
Adding adressing of IPv6 to program 1
malloc() and implicit cast 49
malloc() and implicit cast 7
different ways of allocating memory 3

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top