I have a pointer array defined declared as
char (*c)[20]
When allocating memory using malloc
c=malloc(sizeof(char)*20);
or
c=(char*)malloc(sizeof(char)*20);
I get a warning as “Suspicious pointer conversion”
Why?
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 this declaration
char (*c)[20];cobject has typechar (*)[20].We know that in C
mallocreturn type isvoid *and that there is an implicit conversion betweenvoid *to any object pointer types.So
c = malloc(whatever_integer_expression)is valid in C. If you get a warning, you are probably using a C++ compiler or you are using a C compiler but forgot to include thestdlib.hstandard header.But
c = (char*) malloc(whatever_integer_expression)is not valid C because there is no implicit conversion betweenchar *type andchar (*)[20]type. The compiler has to (at least) warn.