I'm trying to fill an array with names from a file:
Andrew
Andy
Bob
Dan
Derek
Joe
Pete
Richy
Steve
Tyler
Here is the function I wrote...but the program crashes when I run it:
#include <stdio.h>
main(){
int i=0, size=10;
char fname[15];
char** data;
char* name;
FILE* fp;
printf("Enter a filename to read names:\n");
scanf("%s", fname);
fp = fopen(fname, "r");
if(fp == NULL)
exit();
data = (char**)malloc(sizeof(char**)*size);
while(!feof(fp)){
fscanf(fp, "%s", name);
data[i] = (char*)malloc(sizeof(name));
data[i] = name;
i++;
}
fclose(fp);
printf("\n\n");
for(i=0; i<size; i++)
printf("%s ", data[i]);
free(data);
}
Anyone know what I'm doing wrong? Thanks