I am trying to pass a pointer by reference to a function and get that function to point my pointer at a structure that it finds.
char get_structure(STRUCTURE **ptr) { if(foundstructure) { *ptr = &theStructure; return 1; } return 0; }
This works how I expect it, but when I try and declare the function prototype in the header file:
char get_structure(STRUCTURE **ptr);
I always get a compiler error:
error C141: syntax error near '*', expected ')'
I don't get an error when this is a defined type like char, int or long, but I get the error when this is a pointer to a typedef that I have defined.
How can I declare this in my header file without an error?
Nathan
"Adding function.c to the project and changing the include to function.h instead of function.c did not solve the problem."
Had you followed all the instructions, you would have seen that it solved the problem in the code you posted.
"I mentioned in my post that although the example doesn't show it, struct.h does depend on function.h. There is an enum that is declared in function.h that is used in STRUCTURE."
Making your first example a waste of time. Now that you've posted a complete example demonstrating your problem, I've made a few small edits:
Including .c source files is almost always the wrong thing to do.
main.c should include function.h, not function.c. function.h is dependent upon struct.h to fully publish get_struct()'s interface, so should include struct.h. function.c should not be included by anything, so does not need include guards, and needs to be added to the project's list of .c source files for separate compilation and linking.
main.c:
#include "struct.h" #include "function.h" STRUCTURE *structPtr; void main(void) { while(1) { get_struct(&structPtr); } return; }
struct.h:
#ifndef __STRUCT_H #define __STRUCT_H #include "function.h" typedef union _STRUCTURE { unsigned char fields[2]; struct _NAMED { unsigned char field1; ENUMERATION field2; } NAMED; } STRUCTURE; #endif
function.h:
#ifndef __FUNCTION_H #define __FUNCTION_H typedef enum _ENUMERATION { ONE = 1, TWO = 2 } ENUMERATION; #include "struct.h" void get_struct(union _STRUCTURE **myStruct); #endif
function.c:
#include "struct.h" #include "function.h" STRUCTURE myStruct; void get_struct(STRUCTURE **strPtr) { *strPtr = &myStruct; }
One sidenote is that your use of identifiers with leading underscores risks treading on the implementor's reserved namespace.
Duly noted, but those edits do not fix the problem either. Compiler is still throwing the C141 error. Only if I declare the prototype like this do I avoid the error:
void get_struct(union _STRUCTURE **myStruct);
"Only if I declare the prototype like this do I avoid the error:
And the prototype in the function.h I posted is:
How do they differ?
Sorry, I thought you were offering a solution to solve the problem.
Thanks again to Per Westermark.
"Sorry, I thought you were offering a solution to solve the problem."
Oh, good grief!
*plonk*