Strange compile errors in Linux Eclipse C - c

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
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;
}

int *ptr;
int a;
int b; //<- move to block top declaration
a = 20;
ptr = &a;

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.

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.

Related

STM8: local declared pointer on global variable

on my STM8 Disco Board with Cosmic Compiler I tried follwoing code and expected 'ptr_a' and 'ptr_aLocal' to be the same:
int a, b, *ptr_a, *ptr_b;
void main()
{
int *ptr_aLocal;
a = 4;
b = 5;
ptr_a = &a;
ptr_b = &b;
ptr_aLocal = &a;
}
However, 'ptr_a' has the correct address for variable 'a' whereas 'ptr_aLocal' contains a wrong adress.
When compileing the same code with gcc 'ptr_a' and 'ptr_aLocal' are as expected the same.
Whats the problem here when declaring the pointer local in main?
Thank you for your help.
Rafael
Edit:
Now I'm realy confused:
When adding the line 'ptr_b = ptr_aLocal;' at the end of the code, ptr_b shows the correct adress of Variable 'a' (0x106), although 'ptr_aLocal' still contains the wrong adress of variable a (0x108)
enter image description here
Various older versions of misc embedded compilers tended to optimize code somewhat even when optimizations were supposedly disabled. Particularly Cosmic and Codewarrior had such quirks in older versions. Since the local variable isn't used, it is optimized away.
You can see this for yourself by viewing the generated assembler - any half-decent debugger will support assembly step debugging. If there is no stack push or index register store instruction there, then it was optimized away.
You can force the variable to get used with volatile int *ptr_aLocal.
Unrelated to your question, you should use uint8_t as far as possible nad not int, when programming 8 bit microcontrollers.

Error message Variable declaration in C

I'm new to c and I am having a hard time understanding why am I getting an error while trying to compile the following code in c I believe I tried it Java and it worked compiled perfectly without error
void f(void) {
int i;
i = 6;
int j;
j = 20;
}
In "old" C all declarations must be at the top of the functions. In later versions like C99, C declarations can be anywhere in the code. I guess you have an old compiler.
Change your code to
void f(void) {
int i;
int j;
i = 6;
j = 20;
}
The problem is that for some old compilers you need to declare the variables before any executable statement. If you do not want to have this problem, switch to a newer compiler.
If your compiler is configured to compile c code following c98 then you will get this error because following c98 standard the déclaration of variables must be done first then we can make assignment so we can't make declaration of variable in the middle of the code.
However you can select the option to compile your code following the standard c99 and in this case you can make the déclaration of variable in the middle of your code.

Programming in C - Problem

Following is the C Code :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a,b,d;
void Values1()
{
a=a*2;
b=b-5;
}
void Values2(register int c)
{
d+=b-c;
a=a+10;
b*=3;
}
int a,b;
int main()
{
int i=7;
Values2(i);
Values1();
printf("a=%d, b=%d, d=%d\n",a,b,d);
system("pause");
}
It Gives a Compilation Error related to redefinition of 'a' and 'b' in MS Visual C++ 2010.
Please help me in figuring out why this error has occured.
You define a and b in the global scope here:
int a,b,d;
and here
int a,b;
You can not define variables twice.
Your compiler is badly configured. Your program is legal C.
The re-declarations of a and b fall into the category of "tentative definition". A properly configured C compiler, cannot stop compilation because of that.
Why do you have these two lines?
int a,b,d;
int a,b;
You only need to define the variables once. I'd suggest getting rid of the second line.
Some other things you may want to consider in the interests of furthering your skills.
Global variables are usually consider a bad thing. There are better ways to do it which encapsulate information much better, and will reduce the likelihood of problems down the track.
Use meaningful variable names. They only slow down the compiler the tiniest bit and will not affect your running speed, but they make your code so much easier to maintain.
I like to space out expressions to make them more readable as well, like d += b - c; instead of d+=b-c;.
Don't bother with the register keyword, it's a suggestion to the compiler and most modern compilers are far better than developers at figuring out how best to optimise code.
There are two canonical prototypes for main and int main() isn't one of them - use int main (void) if you don't care about program arguments.
On pre-C99 compilers, you should return 0 from main (C99 will return 0 for you if you reach the closing brace of main).
Right after the definition of Values2(), there is the line int a,b; which will conflict with int a,b,d; before Values1(), so you can safely remove the line int a,b;.
Besides, to get good results, you may want to set tha values of a, b and c in the start of the main function to get consistent results.

Gcc compiler C string assignment issue

I wrote this code because I'm having a similar problem in a larger program I'm writing. For all I know the problem is the same so I made this small example.
#include <stdio.h>
typedef struct
{
int x;
char * val;
}my_struct;
int main()
{
my_struct me = {4, " "};
puts("Initialization works.");
me.val[0] = 'a';
puts("Assignment works.");
puts(me.val);
puts("Output works.");
return 0;
}
When compiled with tcc (Tiny C Compiler) it compiles and executes fine. But using GCC 4.6.0 20110513 (prerelease) it compiles, however, when I execute it I only get past "Initialization works." before getting a segfault.
What am I doing wrong? Is it my code or my GCC compiler?
Your code. ANSI permits string constants to be read-only, and this is encouraged because it means they can be shared system-wide across all running instances of a program; gcc does so unless you specify -fwritable-strings, while tcc makes them writable (probably because it's easier).
val is an points to read only location.
char *readOnly = "Data in read only location" ;
readOnly pointing data cannot be modified.
As other answers have pointed out, val is pointing at a string constant. Try
my_struct me = {4, malloc(2)};
and remember to check if val is NULL if you're using this in a real program.

Can't declare variables after statements in DevC++

The trouble here is that I can't declare variables inside a function after the function already has some statements in it. Declaring at the start works fine, but after something, it gives a parse error. For example:
int main()
{
int b;
b = sisesta();
float st[b];
return 0;
}
I'd like to declare an array st with its size being returned by another function, but it won't let me do it! Says "Parse error before float". This is in C by the way, but I guess its identical to what it would be in other languages with the same syntax.
Any help appreciated.
In C standards before C99, you have to declare your local variables at the beginning of the function. Beginning with C99, this is no longer required.
Since Dev-C++ ships with gcc and recent gcc versions do support C99 partially, you can try adding -std=c99 to the gcc argument list in the Dev-C++ settings to trigger C99 mode.
Dude in C you have to declare all variables at the start. You can't declare between statements
you could malloc() a float* to the size you want (just remember to free() it afterwards):
int main()
{
int b;
float *st;
b = sisesta();
if((st = malloc(sizeof float * b)) == NULL){exit 1;}
/* blah blah */
free(st);
return 0;
}
It turns out that I just had an old version of DevC++ which didnt support the newer standard, with the latest release the statements work fine, thanks for the help anyway.
Even in C89, it was just a stylistic choice to do all declarations at the beginning of a function - the trouble that you hit in your code was that you attempted to declare an array on the stack of an unknown size, and that was not allowed until C99. If you were to do the same code, but replace "float st[b]" with a statement where "b" was constant, it would work, like "float st[10]"

Resources