So I have a list of structures and I allocated space to them with malloc()
, using *p
. Now I want to get access to every ptrletter
element and use it. How should I do that? Here's my code.
typedef struct Words {
char *ptrletter;
int numbers;
} Word;
int main(){
FILE *f, *g;
char c,d;
int *a;
int nrofline=0;
int elements=0;
char string[2];
int lines=0;
f=fopen("m_in.txt","r");
do{
d=fgetc(f);
if (d=='\n'){
lines++;
}
}while (d!=EOF);
a=(int*)malloc(sizeof(int)*lines);
rewind(f);
lines=0;
do{
d=fgetc(f);
if ((d>='A' && d<='Z') || (d>='a' && d<='z')){
elements++;
}
if (d=='\n'){
a[lines]=elements;
lines++;
elements=0;
}
}while (d!=EOF);
Word *p=(Word*)malloc(sizeof(Word)*lines);
int j=0;
for (j=0; j<lines; j++){
strcpy(p[j].ptrletter,"");
p[j].numbers=0;
}
rewind(f);
}
I get error at the strcpy()
part (almost the last one) and I have tried to search for an error on google but I haven't found anything useful. It must be dynamically allocated.