Mailinglist Archive: opensuse (1839 mails)

< Previous Next >
Re: [opensuse] C help - compile errors on simple function - need help
  • From: Nick LeRoy <leroy.nick@xxxxxxxxx>
  • Date: Wed, 22 Sep 2010 20:24:59 -0500
  • Message-id: <201009222024.59774.leroy.nick@xxxxxxxxx>

I have a function I just added to a little program the does some
atmospheric calculation and it is giving me errors that variables I have
just defined in the function are unused. My guess is that I'm screwing up
the last 'if' statement because it isn't seeing the P (pressure)

I know this is probably very basic and I'm violating some rule, but this
is a case where I could stare at it and google for hours over something
simple, so I thought I would ask for help curing my CRI.

The build line and the errors are:

Note that these are compiler *warnings*, not errors. But, they are pointing
to a real error in your function. Methinks you would do well to pick up a
book on C programming or a do a little more research and reading on-line.
That being said....

19:08 alchemy:~/dev/prg/ccpp/src-c/prj/test> gcc -o atm -Wall -lm -std=c99
atmtable.c: In function ‘fn_atmPress’:
atmtable.c:173: warning: unused variable ‘M’
atmtable.c:172: warning: unused variable ‘g0’

// Initialize all variable to 0.0
double P = 0.0;
double Pb = 0.0;
double Tb = 0.0;
double Lb = 0.0;
double h = 0.0;
double hb = 0.0;
double R = 0.0;
double g0 = 0.0;
double M = 0.0;

Here, you declare function local variables, including M, g0, etc. Note:
function local variables have scope for the entire body of the function. If
you looked at the line numbers of the warnings, I'm sure that you'd see that
they are the lines numbers of the below declarations, not those above.

// Test SIflag and set variables for Metric or English units
if (SIflag == 1) {
double Pb = arry[3];
double Tb = arry[4];
double Lb = arry[5];
double h = arry[2];
double hb = arry[1];
double R = 8.31432;
double g0 = 9.80665;
double M = 0.0289644;
} else {
double Pb = arry[3];
double Tb = arry[4];
double Lb = arry[5];
double h = arry[2];
double hb = arry[1];
double R = 8.9494596e-4;
double g0 = 32.17405;
double M = 28.9644;

Here, however, you declare a *new* set of variables with a more limited scope
*that happen to have the same names*. The scope of these variables is only
within the enclosing braces "{" and "}". Thus, after the final "}", your
newly scoped variables don't exist. From the compiler's point of view, your
code created some new variables, initialized them, and then never used them.
Hense the warnings. The "double Pb = arry[3];" declares the new variable;
which is most certainly wrong.

Incidentally, it looks like you're mixing tabs and spaces in your code, which
could make edits more difficult in the future.

How to solve the real problem? Easy, don't declare the new set of variables,
just use the ones that you declared at the top of the function (which is I'm
sure what you actually intended to do). The above block should look more like

if ( SIflag == 1 ) {
Pb = arry[3];
} else {
Pb = arry[3];

Even better, you should recognize that the initializations of Pb, Tb, etc. are
all shared, so cleaner code would look like this:

Pb = arry[3];
if ( SIflag == 1 ) {
R = 8.31432;
} else {
R = 8.9494596e-4;

FYIW, you may want to look into other programming languages that are easier to
learn and don't have as many "gotchas". I'm personally a fan of Python.

Hope this helps

To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups