Search code examples
cpointersstructuremodular

How can you use structures in modular programming?


Can you use structures in modular programming in C? And if so, how can you do that? I tried to put p[].ptr in the function headers and it constantly requests and expression before the ']'.

typedef struct Words {
   char *ptrletter;
    int numbers;
} Word;
Word *p=(Word*)malloc(sizeof(Word)*lines);
p[nrofline].ptrletter=(char*)malloc(sizeof(char)*(a[nrofline]+1));
strcpy(p[nrofline].ptrletter,"");
p[nrofline].numbers=0;
fillwordnr(f,p[nrofline].ptrletter,p[nrofline].numbers,lines,nrofline,a,c,string[]);

where

void fillwordnr(FILE *f, char letters[], int numbers[],  int lines, 
int nrofline, int *a, char c, char string[]){
do {
    c=fgetc(f);
    if ((c>='A' && c<='Z') || (c>='a' && c<='z')){
        string[0] = tolower(c);
        string[1]='\0';
        strcat(letters[nrofline],string);
    }
    else if (c>='0' && c<='9') {
        string[0]=c;
        string[1]='\0';
        numbers[nrofline]=(numbers[nrofline])*10+(c-'0');
    } else if (c == '\n'){
        string[0]='\0';
        strcat(letters[nrofline],string);
        nrofline++;
        if (nrofline<lines){
            letters[nrofline]=(char*)malloc(sizeof(char)*(a[nrofline]+1));
            numbers[nrofline]=0;
            strcpy(letters[nrofline],"");
        }
    }
}while (c!=EOF);
}

*** Okay people the problem is that it doesn't compile? Because it gives the error <>

I put nroflines between the brackets and it still gives the SAME error.


Solution

  • Well, this is a problem:

    fillwordnr(f,p[].ptrletter,p[].numbers,lines,nrofline,a,c,string[]);
    

    You have to specify which element of p you want to work with - p[0], p[1], p[i], etc. Assuming nrofline is a valid index for p, you would write

    fillwordnr( f, p[nrofline].ptrletter, p[nrofline].numbers, lines, nrofline, a, c, string );
    

    Honestly, based on the code you've posted, it's not clear what you're trying to accomplish.