What's wrong with "char *p = malloc(10);" ?
Answers were Sorted based on User's Feedback
Answer / guest
malloc returns a void pointer. It needs to be cast to char*
before allocating it to char* variable.
char *p = (char *)malloc(10); should do fine.
Is This Answer Correct ? | 26 Yes | 2 No |
Answer / poornima
Nothing wrong.Its fine..
if u hve further doubt run the below code.
#include<stdio.h>
int main()
{
char *p=malloc(10);
if(p==NULL)
{
printf("malloc failed");
exit(0);
}
else
{
printf("malloc success\n");
p="hai";
printf("%s",p);
}
return 0;
}
o/p:
malloc success
hai
Is This Answer Correct ? | 6 Yes | 10 No |
Answer / guest
Function calls are not allowed in initializers for global or
static variables.
Is This Answer Correct ? | 3 Yes | 12 No |
Answer / splurgeop
malloc return an adress to the free memory..so we shud
store the adress i.e.
the above should be
char p=malloc(10);
Is This Answer Correct ? | 1 Yes | 16 No |
what is the difference between declaration and definition of a variable or function ?
What is the advantage of an array over individual variables?
write a sorting prgm to sort 50 nos and sum them and also remove all the occurrences of 15 and print it?
Why c is called a mid level programming language?
diff between exptected result and requirement?
What is %d called in c?
what are the stages of compilation
while loop contains parts a) initialisation, evalution of an expression,increment /decrement b) initialisation, increment/decrement c) condition evalution d) none of the above
implement NAND gate logic in C code without using any bitwise operatior.
How can I find the modification date of a file?
what are the files which are automatically opened when a c file is executed?
When should a type cast not be used?