I'm trying to understand how to populate an array of pointers to strings programatically, and not have to initialize them in the code.
For example, the following works OK:
and when I try this, I get a segmentation fault from the strcpy.Code:int i; char * NameArray[3] = {"Mike","Carter","Dale Z."}; for (i=0; i<3; i++) { printf("Name %d is %s\n", i, NameArray[i]); }
Is there any way to enter the data programatically using the array of pointers to char? I got it to work declaring the array as char NameArray[3][20] but was looking to use pointers.Code:int i; char * NameArray[3]; char MyInput[20]; for (i=0; i<3; i++) { printf("Enter name:"); scanf("%s", MyInput); strcpy(NameArray[i], MyInput); printf("Name %d is %s\n", i, NameArray[i]); }



LinkBack URL
About LinkBacks


