N
nick
i do not know what is the use of (e.g. void *pt), when will use it.
thanks!
thanks!
Viv said:You should use it when you do not know what data type the memory
contains. The advantage is that once u know the data type, u may
typecast the void ptr into the appropriate data type.
Sandeep said:A good example is the "malloc" function.
It returns a void* and you can
typecast it to whatever type you need.
Prototype : void *malloc(size_t size);
Usage : int *n = (int *) malloc(sizeof(int));
Sandeep said:A good example is the "malloc" function. It returns a void* and you can
typecast it to whatever type you need.
Prototype : void *malloc(size_t size);
Usage : int *n = (int *) malloc(sizeof(int));
enough to have seen the generally recommended usage of
int *n = malloc(sizeof *n);
You don't need to cast the return value of malloc, and doing so can
prevent the compiler from warning you about not having a declaration of
malloc in scope. Also, the form you showed means having to get the type
correct three times instead of once, having to check it in three places,
and having to change it in three places instead of one if it needs changing.
I was trying to explain the use of "void *"
and though that to a newbie
it would have been easier to understand it as
int *n = (int *)malloc(sizeof(int));
But Point Taken,
I should not explain one concept in a way that gives a
not so correct information about some other concept.
pete said:It's not some other concept.
The lack of cast
is the reason for the existence of the pointer to void type.
pete said:It's not some other concept.
The lack of cast
is the reason for the existence of the pointer to void type.
Previous to the invention of the pointer to void type,
the pointer to char type was used to accomplish the same tasks.
It's not some other concept.
The lack of cast
is the reason for the existence of the pointer to void type.
Previous to the invention of the pointer to void type,
the pointer to char type was used to accomplish the same tasks.
K & R - Second Edition
p = ( char *) malloc(strlen(s) + 1) ;
K & R
Section 7.8.5
The pointer returned by malloc has the proper alignment for the object
in question, but it must be cast into the appropriate type.
Things might have changed a lot, but
int *p = (int *) malloc ( sizeof(int) ); , is NOT a SIN.
K & R - Second Edition
PS : "void *" existed at the point this edition went into press.
i do not know what is the use of (e.g. void *pt), when will use it.
You should use it when you do not know what data type the memory
contains. The advantage is that once u know the data type, u may
typecast the void ptr into the appropriate data type.
Mark said:Read the errata.
which specifically mentions that the cast was a mistake...
Ben Pfaff has I believe a useful page all about hte sinfulness of
casting the return from malloc.
A constant 0 or any expression resulting in 0,
when casted into (void*) is known as a NULL ptr.
pete said:Not exactly.
NULL, is a macro which expands to a null pointer constant.
Any constant expression
with an integer type that compares equal to zero,
is already a null pointer constant,
with or without the (void *) cast.
When you write "NULL pointer"
nobody knows whether you mean
1 the NULL macro
2 a null pointer constant
3 a null pointer
Viv said:You should use it when you do not know what data type the memory
contains. The advantage is that once u know the data type, u may
typecast the void ptr into the appropriate data type.
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.