user1795374
user1795374

Reputation: 87

Expected identifier error in C

For some reason I am getting the error:

expected identifier or '(' before 'wordlist'

in my header file (as well as the corresponding function definitions) for the two functions returning wordlist pointers.

With the following code:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

typedef struct word{
   char *string;
   struct word* next;
}word;

typedef struct wordlist{
   word *head;
   word *tail;
}wordlist;

*wordlist populateList(FILE *file);

*wordlist encrypt(wordlist *wl, int rotation);

void toFile(wordlist *wl, char *outputFileName);

#endif

Can anyone tell me why this might be?

Upvotes: 1

Views: 2627

Answers (3)

Richard Chambers
Richard Chambers

Reputation: 17573

If you want to define or declare a variable you specify the type of the variable followed by the variable name. So if you want a variable of type wordlist you would use:

wordlist myVariable;

If you want to specify a variable to be a pointer to a variable type, you prefix the variable name with an asterisk so if you want a variable that is a pointer to a variable of type wordlist you would use:

wordlist *myVariable;

The reason why most experienced C programmers put the asterisk with the variable name is because of something like the following:

wordlist myVariable, *pVariable1, myVariable2, *pVariable2;

The above will create four variables. myVariable is of type wordlist. myVariable2 is of type wordlist. pVariable1 and pVariable2 are of type pointer to wordlist.

So the asterisk acts as a kind of adjective or qualifier or modifier for the variable name declaration indicating that the variable is not of the type specified but is instead a pointer to the type specified.

The combined variable definition is the same as the following four lines of definitions.

wordlist myVariable;  // declares a variable of type wordlist
wordlist *pVariable1; // declares a pointer to a variable of type wordlist
wordlist myVariable2; // declares a variable of type wordlist
wordlist *pVariable2;  // declares a pointer to a variable of type wordlist

Function definitions/declarations work similarly.

wordlist *myFunc (void) {
    wordlist *myNew = malloc (sizeof(wordlist));

    if (myNew) {
        // set up myNew stuff
    }

    return myNew;
}

Edit: function pointers

You can also specify a variable that contains a function pointer. For instance for myFunc() above you might specify something like the following. Notice that I am using parenthesis to enforce a specific order of evaluation. What this says is that pFunc is a pointer to a function that does not accept arguments (void argument list) and which returns a pointer to a wordlist variable. There are rules about operator and modifier precedence in C however as expressions become more complicated, it is usually better to enforce an evaluation order using parenthesis. See Programs as Data: Function Pointers

wordlist *((*pFunc) (void)) = myFunc;   // pointer to a function that returns a pointer to a wordlist

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

This is because when you declare a pointer, the asterisk must follow the type name, not precede it:

wordlist * populateList(FILE *file);
//       ^
//       |
//      Here

Upvotes: 1

Matteo Italia
Matteo Italia

Reputation: 126777

If in populateList and encrypt you want to return a pointer to wordlist, the correct syntax is wordlist *, not *wordlist (it's exactly as it is everywhere else).

Upvotes: 0

Related Questions