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 adt in c programming?
Write a program to generate the first n terms in the series --- 2,3,5,7,11,...,17
i have to apply for the rbi for the post of officers. i need to know abt the entrance questions whether it may be aps or techinical....
Explain the process of converting a Tree into a Binary Tree.
What are the types of data structures in c?
Why & is used in c?
write a program to find the largest and second largest integer from an array
How can I copy just a portion of a string?
difference between my-strcpy and strcpy ?
3 Answers Geometric Software, IIM, Infosys,
What are external variables in c?
What are the string functions? List some string functions available in c.
what is the use of call back function in c?tell me with example