-7

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.

Lilla Nagy
  • 91
  • 1
  • 2
  • 11
  • 2
    "I tried to put p[].ptr in the function headers" <- what does that mean? – user253751 Jan 12 '16 at 23:14
  • 1
    What is modular programming? – user253751 Jan 12 '16 at 23:14
  • Show your code, so we'll be able to tell you what's wrong. – mikedu95 Jan 12 '16 at 23:16
  • modular programming means that you use more files. in one file you have the main() and in the other you have all the other functions you're using, then you're connecting them with a header. – Lilla Nagy Jan 12 '16 at 23:16
  • 2
    Your problem appears to have nothing much to do with modular programming but rather basic syntax errors. Show your code if you want help with that. – kaylum Jan 12 '16 at 23:17
  • 1
    `p[].ptrletter` <- so which p's ptrletter do you want? You have to put something between the brackets. – user253751 Jan 12 '16 at 23:21
  • @immibis i edited it, now there's the code too :P – Lilla Nagy Jan 12 '16 at 23:21
  • Is `Word *p=(Word*)malloc(sizeof(Word)*lines);` and so on in a header file (.h file)? If so, it shouldn't be. – user253751 Jan 12 '16 at 23:21
  • I tried to put nrofline between the brackets but it didn't work – Lilla Nagy Jan 12 '16 at 23:22
  • @immibis no it's not. it's only in the main file – Lilla Nagy Jan 12 '16 at 23:22
  • *"I tried to put nrofline between the brackets but it didn't work"*: What does mean "it didn't work" ? What is the behavior that you expect and what doy you get instead ? – mikedu95 Jan 12 '16 at 23:25
  • it gives the same error – Lilla Nagy Jan 12 '16 at 23:26
  • "I tried to put nrofline between the brackets but it didn't work". I didn't work because...?? There are a number of clear problems as well as potential problems with your code. But it is not productive to comment on them at this stage as you have not provided all the required information for a full answer. Please post a [Minimal Complete and Verifiable Example](https://stackoverflow.com/help/mcve) including exactly what errors or problems you are encountering. – kaylum Jan 12 '16 at 23:27

4 Answers4

0

Yes, you can use structures with in a program that consists of multiple files.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Normally I wouldn't post an answer this short, but I felt like making a point about asking good questions. The question in this case is "Can you do this?", not "How do you do this?" – user253751 Jan 12 '16 at 23:17
0

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • I tried doing that, but it gives the same error. I read a txt file and in this function I separate the letters from the numbers into two arrays. for example alba43tros would be albatros and 43. – Lilla Nagy Jan 12 '16 at 23:29
  • @LillaNagy: Is `nrofline` declared in the same module from which you are calling `fillwordnr`? – John Bode Jan 12 '16 at 23:36
  • it's declared in the function header and in the main file – Lilla Nagy Jan 12 '16 at 23:37
  • @LillaNagy: Are you calling `fillwordnr` with the last parameter `string[]` or `string`? – John Bode Jan 12 '16 at 23:39
0

C syntax.

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

it constantly requests and expression before the ']'.

Of course it does. Elsewhere in your code, the [] in int variableName[] is describing the type of variable. An array. Similar to int* variableName, which is a pointer to an int. The data types are different. When trying to use the array, it doesn't want to know what type of data structure it is, it wants the actual data. When you're calling p[] you're not sending ALL of the data structure. You're not even just sending one. You're sending the type of a data structure. And p[].herp is gibberish.

What you want to send into the function is something like:

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

Or if you want it to be done to the entire data set:

for(int i=0; i<numWords; i++)
{
  //...
  fillwordnr(f,p[i].ptrletter,p[i].numbers,lines,nrofline,a,c,string);
  //...
}

And for all that is holy, get a better naming system rather than a, c, string as the name of a string, numbers as the name for an int. It'll save you and anyone looking at the code a world of headache. Naming is important.

Philip
  • 1,539
  • 14
  • 23
  • I will try that, thanks :) I will edit the names and everything else once the program compiles. This was edited from an already working program so it should work once I edit these modules appropriately. – Lilla Nagy Jan 12 '16 at 23:39
  • I did this fillwordnr(f,p[0].ptrletter,p[0].numbers,lines,nrofline,a,c,string); and it still gave the same error – Lilla Nagy Jan 12 '16 at 23:41
0

Okay I found the error. Thanks everyone for helping!

So I had to do what Philip said, namely:

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

and what John Bode said (I put string[] instead of string).

Now it doesn't give an error anymore. Thank you!

Lilla Nagy
  • 91
  • 1
  • 2
  • 11
  • And sorry for asking stupid questions but I'm a beginner – Lilla Nagy Jan 12 '16 at 23:47
  • Yeah, it doesn't give you an error anymore.... but I don't think it does what you want it to do. You have `p` as a `word` array of size... `lines`... whatever that means. p[0] is just the first one. Don't worry about the question. Everyone starts somewhere (I remember going through the exact same issue years back). As a beginner 1) learn to ask better questions. See kaylum's comment and link. 2) stay away from malloc. Just use fixed-size arrays starting out, no need to complicate things. 3) Naming conventions. 4) Upvote. I covet these magical internet points. – Philip Jan 13 '16 at 15:22
  • Sorry to answer only now, but back then when I needed this, we were learning to use malloc, and I was super confused about memory allocations and pointers and all that. Thank you for your kind answer though :) I always get tons of downvotes and I never understand why, so this is helping :) – Lilla Nagy Nov 19 '16 at 14:50