2

I am trying to detect an error so I included in my program some traces. The problem is that after that, it doesn't compile, giving me next error:

../src/DR700_API.c:46: parse error before `*'

I just added an fprintf at the beginning of each function:

fprintf(stdout,"_name_of_function_");

Commenting all fprintf it compiles right, so there's the error. I can't dispense with them as i want to track other error in execution time.

Here's a little example:

#include <stdio.h>                                                                                                                          
#include <stdlib.h>

ImprFunc *DR700_new()                                                                                                                       
{                                                                                                                                           
    fprintf(stdout,"DR700_new");                                                                                                           
    ImprFunc *impr = (ImprFunc *)malloc(sizeof(DR700_ImprFunc));                                                                            
    if (impr == NULL)                                                                                                                       
        return NULL;  
...


../src/DR700_API.c:46: parse error before `*'
../src/DR700_API.c:47: `impr' undeclared (first use in this function)
../src/DR700_API.c:47: (Each undeclared identifier is reported only once
../src/DR700_API.c:47: for each function it appears in.)
make: *** [../obj/DR700_API.o] Error 1
Joster
  • 359
  • 1
  • 4
  • 19

2 Answers2

2

Probably your setup doesn't allow mixed code and declarations (as per C89). If you wish to not affect project setup - try to keep declarations before any code. In your example it means

ImprFunc *impr = (ImprFunc *)malloc(sizeof(DR700_ImprFunc));
fprintf(stdout,"DR700_new");

instead of

fprintf(stdout,"DR700_new");
ImprFunc *impr = (ImprFunc *)malloc(sizeof(DR700_ImprFunc));

Or alternatively - add -std=c99 (as was mentioned in comments).

Sergio
  • 8,099
  • 2
  • 26
  • 52
2

In early versions of C, variables had to be declared at the beginning of a block.

C99 allows to mix declarations and statements arbitrarily (e.g. see Variable declaration placement in C and Where can I legally declare a variable in C99?).

You could try compiling with --std=c99 / --std=c11 which will allow you to declare variables anywhere (if supported in your version of gcc. See Status of C99 features in GCC and C11Status).

C99 is, for the most part, backward compatible with C89.

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126