July 10, 2011

How to use strcpy with malloc function

 char s[]="Hello" is an array of chars  
 char *s[] = "asa"; is an error  
 coz itz an array of character pointers  
 char *s[] = { "AAA", "BBB", "CCC"}; is OK  
 char s[]="ABCD"  
 char (*p)[5] = &s; //is fine... here p is a pointer to an array  
 In Your case... U cn do this  
 char s[] = "Hello"  
 char *p[2];  
 p[0] = (char *)malloc(sizeof(s)); strcpy(p[0], s);  
 p[1] = (char *)malloc(sizeof(s)); strcpy(p[1], s);