Im presently learning pointers to strings (From "Lets Us C" by Yashwant Kanetkar). While explaining arrays of pointers to strings, the book says that we cannot recieve strings through pointers as shown below:
CODE
main()
{
char *names[6];
int i;
for(i=0;i<=5;i++)
{
printf("\nEnter name");
scanf("%s",name[i]);
}
}
Its says that u can get out of this problem like this
CODE
#include<malloc.h>
#include<stdio.h>
#include<string.h>
void main()
{
char *names[6];
char n[50];
int len, i;
char *p;
for(i=0;i<=5;i++)
{
printf("\nEnter Name");
scanf("%s",n);
len=strlen(n);
p=malloc(len+1);
strcpy(p,n);
names[i]=p;
}
for(i=0;i<=5;i++)
printf("\n%s",names[i]);
}
now this program gives da following error:
cannot convert from 'void *' to 'char *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast.
wat does this mean an wat do i do about it.
also i havent understood the use of malloc() here. n wats da need of saying:
CODE
p=malloc(len+1);
strcpy(p,n);
names[i]=p;