I am trying to understand how to use malloc, calloc and realloc.
I am getting the following error for below code: error C2440: '=' : cannot convert from 'void *' to 'char *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
#include <stdio.h>
#include <stdlib.h>
main()
{
char mychar[]= "This is a test";
char *p;
p=malloc(sizeof(mychar)+ 20);
if(p== NULL)
printf("Error: memory allocation did not work!");
printf("%d", sizeof(mychar));
return 0;
}
What is wrong?
I am getting the following error for below code: error C2440: '=' : cannot convert from 'void *' to 'char *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
#include <stdio.h>
#include <stdlib.h>
main()
{
char mychar[]= "This is a test";
char *p;
p=malloc(sizeof(mychar)+ 20);
if(p== NULL)
printf("Error: memory allocation did not work!");
printf("%d", sizeof(mychar));
return 0;
}
What is wrong?