-1

I'm hoping someone can help me out with this. I'm a Linux & Eclipse noob, but I'm pretty familiar with C/C++, though its been a while since I've used them. When I try to compile I get strange errors. No matter what I do to fix them they don't seem to go away.

You can see the there's a simple main function with a little bit of code. There's only 15 lines of code but if you look at the errors they are in external libraries, stdio.h. In main it says there's one error at line 11 but that one doesn't make sense. I assume it's an Eclipse settings problem, but I have no idea what to do to fix it. Any help would be very appreciated. By the way I'm using SciLinux and Eclipse Indigo Service Release 2. Thanks enter image description here

Code:

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

int main(void)
{
   int *ptr;
   int a;
   a = 20;
   ptr = &a;
   int b;
   b = *ptr;
   printf(" ptr is %d\n",b);
   return 0;
}
Kijewski
  • 25,517
  • 12
  • 101
  • 143
Frank Dejay
  • 689
  • 5
  • 13
  • 17
  • 7
    It would be infinitely preferable if you paste the actual code and the actual error messages in your question, rather than a screenshot (which is hard to read). – Oliver Charlesworth Apr 07 '12 at 22:32
  • Looks to me like the Compiler cannot find its include files. Check your Eclipse settings and double check how Eclipse calls the compiler. – cli_hlt Apr 07 '12 at 22:32
  • @cli_hlt: Pretty sure it *is* finding the include files, because it's complaining about specific lines inside them. – Oliver Charlesworth Apr 07 '12 at 22:36
  • Also, depending which version of C you're compiling for (And how strict your compiler is being), it may be complaining about the fact that you've declared the variable `b` in the middle of your code instead of at the beginning of the function. The restriction doesn't exist for C++, and AFAIK had been relaxed for C99. – Ben Cottrell Apr 07 '12 at 22:37
  • 1
    It should also be pointed out that according to that screenshot, you haven't saved the file, which could explain the weird mismatch. – Oliver Charlesworth Apr 07 '12 at 22:38
  • Maybe `ptr` is typedef'd in a header file? Your C also looks invalid, including declarations after statements (`int b;` after `ptr = &a;`, that's not standard C, although many compilers accept it). – Keith Randall Apr 07 '12 at 22:38
  • 1
    @KeithRandall: That's standard C99. – Oliver Charlesworth Apr 07 '12 at 22:39
  • 1
    According to screenshot, that is a C program, not a C++ program. Deleting C++ tag. – Robᵩ Apr 07 '12 at 22:44
  • @OliCharlesworth I added the screen shot because I believe it's an Eclipse issue I'm having. I'm pretty use to using Visual Studio on Windows. This is just one example, maybe not the best. But it seems that I clear the errors and when I recompile they still show up. – Frank Dejay Apr 07 '12 at 22:49
  • ems that I clear the errors and when I recompile they still show up. For instance if you look at the error for line 11 it makes no sense. It says **expected identifier or "(" before "/" token** even though those tokens aren't anywhere near line 11. – Frank Dejay Apr 07 '12 at 22:57

3 Answers3

0
int *ptr;
int a;
int b; //<- move to block top declaration 
a = 20;
ptr = &a;
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

Some of the previous compiler have this weird problem relating to C they only accept variables which are declared in the beginning of the function.

So most probably the error is because you have not declared the variable b at the starting of the block , i suggest you try using a different compiler or be prepared to declare all the variables at the beginning.

Desert Ice
  • 4,461
  • 5
  • 31
  • 58
0

As other answers say, mixing code and declarations is illegal in old fashioned plain C. See:

Variable declaration placement in C

How to enforce C89-style variable declarations in gcc?

In eclipse, the standard version used will depend on the compiler flags passed to the C compiler gcc: either -std=c89 or -std=c99. Depending on how the project is set up, will either be in the Eclipse project properties or a Makefile.

Community
  • 1
  • 1
soru
  • 5,464
  • 26
  • 30