Is there any benefit of casting NULL to a struct pointer in C ?
For example:
typedef struct List
{
....
} List;
List *listPtr = ((List *) NULL) ;
Example from PostgreSQL source:
#define NIL ((List *) NULL)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In assignment example the explicit cast make no useful sense. However, it seems that you the question is really about
#define NIL ((List *) NULL)macro, whose usability extends beyond assignment.One place where it might make sense is when you pass it to a variadic function or to a function declared without a prototype. The standard
NULLcan be defined as0or0Lor((void *) 0)or in some other way, meaning that it might be interpreted differently in such type-less contexts. An explicit cast will make sure that it is interpreted correctly as a pointer.For example, this is generally invalid (behavior is undefined)
while replacing the call with
makes it valid.