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 types of bitwise operator?
how do you execute a c program in unix.
what is the little endian and big endian?
Determine if a number is a power of 2 at O(1).
List the variables are used for writing doubly linked list program.
Design a program using an array that searches a number if it is found on the list of the given input numbers and locate its exact location in the list.
What does s c mean in text?
main() { static int ivar=5; printf("%d",ivar--); if(ivar) main(); }
Write a program to find the smallest and largest element in a given array in c language
What are the types of assignment statements?
GIven a sequence of characters. How will you convert the lower case characters to upper case characters. ( Try using bit vector - sol given in the C lib -> typec.h)
What is the difference between c and python?