I am getting this error within C.
error: expected ')' before '*' token
But cannot trace it.
void print_struct(struct_alias *s) //error within this line
{
...
} //end of print method
My question is when receiving this error where can the error stem back to? Is it a problem with the function, can it be an error with what is being passed in? What is the scope of the error?
The compiler doesn't recognize the name struct_alias as a type name.
For that code to compile, struct_alias would have to be declared as a typedef, and that declaration would have to be visible to the compiler when it sees the definition of print_struct.
(Typedef names are tricky. In effect, they become temporarily user-defined keywords, which is why errors involving them can produce such confusing error messages.)
This is not specific to C89; it applies equally to C90 (which is exactly the same language as C89), to C99, and to C11.
The error means that here's no such type as struct_alias declared in this translation unit.
Related
I have the following code in my project:
printf("Please select one of the tests: ");
int n;
scanf("%d", &n);
(void (* [])()) {test1, test2, test3, test4}[n - 1]();
For me, this code compiles and works as indented. But my professor said that this doesn't compile for her.
I write code compliant with the C23 standard and my compiler is Apple Clang v13.0.0. But about my professor, all I know is that she uses the MSVC compiler; I have no information on either the compiler version or the standard she marks the code with.
I tried changing the C standard to C99 and it still worked, so I think this has to be a compiler issue. I don't have a Windows machine to play around with MSVC and test whether the problem is the anonymous array initialization, the subscript operator immediately after it, the call operator after the entire thing, or something else entirely.
Now, of course I know that I can declare an array of function pointers first, and then assign every element, or just use a switch for this purpose. I also know that this is a kind of "clever code" that may not be welcome in actual projects maintained by more people, but this is my code, created for educational purposes only. Also, my professor likes this kind of "clever tricks" as extreme examples of what can you do with the language. What I do like to know is what could be the reason this code does not compile with MSVC. Is that syntax some compiler-specific language extension or is it just Microsoft which, as always, is not keeping up with support for all language features?
Your teacher is probably compiling your code as C++.
With dummy functions added, if this code is placed in a .c file and compiled with MSVC 2015, it compiles fine. If the file has a .cpp extension, it generates the following errors:
x1.cpp(13): error C3260: ')': skipping unexpected token(s) before lambda body
x1.cpp(13): error C2143: syntax error: missing ';' before '}'
x1.cpp(13): warning C4550: expression evaluates to a function which is missing an argument list
x1.cpp(13): error C2676: binary '[': 'main::<lambda_8caaf9f5b122025ad6cda2ca258a66a7>' does not define this operator or a conversion to a type acceptable to the predefined operator
x1.cpp(13): error C2143: syntax error: missing ')' before ';'
x1.cpp(13): error C2059: syntax error: ';'
So it thinks your compound literal is a lambda function. Compound literals are a C-only construct, so unless your C++ compiler supports them as an extension it won't work.
Have your teacher compile your code as C by putting it in a file with a .c extension.
Alternately, don't use a compound literal. Create the array first, then index it and call the function.
void (*f[])() = {test1, test2, test3, test4};
f[n - 1]();
I am making a rogue-like game in C and I am having trouble with file linkage.
I am making a custom header file where I declare an array of structures, but when I compile this code:
#ifndef spells
#define spells
struct spells SpellList[55];
#endif // spells
I get an error: Expected identifier or '(' before '[' token.
You are using the identifier spells for two different purposes: as a "guard macro" for the header file, and as the tag-name for a struct. The compiler does not understand that you want these to be independent. With the code as shown, the preprocessing stage will replace all uses of the identifier spells with nothing, and then the parsing stage will see
struct SpellList[55];
which is invalid.
You must rename either the guard macro or the struct tag. Since you never need to refer to guard macros anywhere else, it's probably easiest to rename the guard macro.
Incidentally, "rouge" is a type of make-up. The kind of game you are making is a rogue-like.
I think the problem is using your defined symbol spells as a type.
Where you have:
struct spells SpellList[55];
The preprocessor will replace spells in that line with the value of the spells define (nothing) before the compiler tries to compile the code. This produces invalid code.
I'm getting a strange compilation error for a C code in MSVC only. More precisely:
error C2143: syntax error : missing ';' before 'type'
C2143 is a fairly generic error, and there are myriad of questions on SO around it, but none of them seems to apply so far.
The closest one can be found here, and stress the importance of declaring variables at the beginning of a block, which seems to have been respected here.
Here is a sample code:
#define NB_LL 6
typedef struct { long long ll[NB_LL ]; } stateSpace_t;
#define ALLOCATE_ONSTACK(stateName) stateSpace_t stateName##_s; void* stateName = (void*) &(stateName##_s);
The following code works well:
void f1()
{
ALLOCATE_ONSTACK(state1);
/* do something */
}
This one doesn't:
void f2()
{
ALLOCATE_ONSTACK(state1);
ALLOCATE_ONSTACK(state2); // <--- error C2143: syntax error : missing ';' before 'type'
/* do something */
}
The second code works well with GCC, so the issue seems restricted to MSVC.
My understanding is that macro ALLOCATE_ONSTACK() only do variable declaration and initialization, so it seems to respect C syntax.
Is it?
OK, this one is fairly convoluted.
Look at the
#define ALLOCATE_ONSTACK(stateName)
It ends with a ; character.
Now look at your code :
ALLOCATE_ONSTACK(state1);
It also ends with a ';' character. That means that, on this particular line, you have 2 following ';' characters.
Since MSVC is not C99, it requires all declarations to be done at the beginning of the block. Since you have two ';' characters following each other, it acts as if the declaration area was ended. So, when you declare other variables in :
ALLOCATE_ONSTACK(state2);
it then fails, syntax error.
GCC has no such problem since it is C99.
Either remove the ';' character at the end of the macro, or within your source code. Only one is required. Not sure which solution is better...
[Edit] : As suggested in comments and other answer, removing the semicolon from the macro looks the better solution.
You have a semi-colon at the end of the ALLOCATE_ONSTACK macro definition and also at the end of the invocation. This means that you effectively have a null statement after each macro expansion. Because of this, the second expansion is not at the beginning of the block.
Classically, C has required that all declarations occur in a block before the first non-declaration statement. gcc relaxes this requirement so the error doesn't occur.
I'd suggest rewriting your macro definition without the trailing semicolon.
Edit: beat to the punch.
All identifiers in C need to be declared before they are used, but I can`t find where it denoted in C99 standard.
I think it refers to macro definitions too, but there is only macro expansion order defined.
C99:TC3 6.5.1 §2, with footnote 79 explicitly stating:
Thus, an undeclared identifier is a violation of the syntax.
in conjunction with 6.2.1 §5:
Unless explicitly stated otherwise, [...] it [ie an identifier] refers to the
entity in the relevant name space whose declaration is visible at the point the identifier
occurs.
and §7:
[...] Any other identifier has scope that begins just after the completion of its declarator.
There are a at least couple of exceptions to the rule that all identifiers need to be delcared before use:
while C99 removed implicit function declarations, you may still see C programs that rely, possibly unknowingly, on them. There is even the occasional question on SO that, for example, ask why functions that return double don't work (when the header that includes the declaration of the function is omitted). It seems that when compiling with pre-C99 semantics, warnings for undeclared functions are often not configured to be used or are ignored.
the identifier for a goto label may be used before it's 'declaration' - it is declared implicitly by its syntactic appearance (followed by a : and a statement).
The exception to the rule for goto labels is pretty much a useless nitpick, but the fact that function identifiers can be used without a declaration (pre-C99) is something that can be useful to know because you might once in a while run into a problem with it as a root cause.
Also, identifiers can be used before being 'declared' (strictly speaking, before being defined) in preprocessing, where they can be tested for being defined or not, or used in preprocessor expressions where they will evaluate to 0 if not otherwise defined.
I'm trying to complete a project for school involving the use of semaphores. I have included the proper header files ( plus one for pthreads). I have pointed the compiler to the proper libraries as well. This is written in C. Yes, this is an assignment, but please be aware I am not looking for help with implementation, rather I can't seem to figure out this damnable compile error.
Here are lines 47 through 50 of my code, which are "simple" declarations of the semaphores and initializing them:
sem_t empty;
sem_init(&empty, 0, 5);
sem_t full;
sem_init(&full, 0, 0);
Here are the messages I'm getting when I try and compile for line 48. I get the same set for line 50 but didn't post it for the sake of brevity:
|48|error: expected declaration specifiers or ‘...’ before ‘&’ token|
|48|error: expected declaration specifiers or ‘...’ before numeric constant|
|48|error: expected declaration specifiers or ‘...’ before numeric constant|
|48|warning: data definition has no type or storage class|
|48|warning: type defaults to ‘int’ in declaration of ‘sem_init’|
I have declared all these outside the main() function. How can I resolve these errors? I'm baffled because it seems to indicate no data type for sem_t, but it is defined in semaphore.h, which I have included. I am compiling this using Code::Blocks under Ubuntu, which is using gcc. This error occurs even when compiling from the command line.
Thanks in advance for the help.
I think your problem may be related to scoping.
"I have declared all these outside the
main() function"
sounds suspicious, because I can see you calling a function right after the declaration.
Try moving the calls to sem_init inside main
You can declare things at file scope (i.e. outside of main, effectively creating a global variable) but you can't call functions (like sem_init) at file scope. They must be called at function scope (e.g. inside of main())