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 are the advantage of c language?
Identify the operators that is not used with pointer a. && b. # c. * d. >>
What are the advantages and disadvantages of c language?
What does %p mean?
What are operators in c?
I heard that you have to include stdio.h before calling printf. Why?
What is meant by realloc()?
I want tcs placement papers of 2004-2009 , its urgent
What is structure padding in c?
How can I make sure that my program is the only one accessing a file?
int main() { int x = (2,3,4); int y = 9,10,11; printf("%d %d",x,y); } what would be the output?
Explain the priority queues?