error in code

S

Seebs

Picking up hints to write better C code, improving my English and even
learning something about sheet music. And to think some say Internet is
nothing but a waste of time. ^^

You're right, sheet music is important. :p

-s
 
B

Bill Cunningham

Geoff said:
mean(a, sizeof(a) / sizeof(a[0]));

What's the sizeof(a[0]) mean?

It's the size, in bytes, (whatever they are), of the first element of
the array "a". It's better than writing sizeof(double) because if you
change the declaration of "a" from double a[] to float a[] you don't
have to change the calculation of the size.

sizeof(a) / sizeof(a[0]) is "the total length of the array divided by
the length of any member of the array" (i.e., the count of the members
of the array).

Oh Ok I see.
 
J

jacob navia

Le 28/05/11 21:28, Bill Cunningham a écrit :
I am trying here to calculate a mean of 3 values. I do not understand
the errors the compiler returned I've never seen them.

#include<stdio.h>

void mean(double *avg, int periods)
{
double val;
val = *avg / periods;
printf("%.2f\n", val);
}

int main(void)
{
double a[] = 2.2, 4.2, 5.00;
mean(a, 3);
}
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

What's it trying to tell me?

Bill

Thanks a lot Bill.

For unknown reasons (normally I ignore your threads) I typed your code
and compiled it with lcc-win.

NO ERRORS.

WHAT?

Yes, lcc-win issued NO ERRORS AT ALL.

After a lot of work I found that I introduced a bug by eliminating a
call to "error" when after an equals sign in an array initialization
I do not find an opening brace.

Why did I do that?

Forgot, this bug is there since at least 3-4 years...

Well, thanks for your questions.

jacob
 
B

Bill Cunningham

Lew said:
Bill, you've done it before. Why can't you write a "cycle" (aka a
"loop") this time?

I know but its been a long long time. I got away from C for a long time
awhile back. Burned out or something. And remember I am a hobyist. I have
other things in my life besides C.
Think
for (....) {/*something*/}
or
while (....) {/*something*/}
or
do {/*something*/} while (....);

It's these little
caveats in C that confuse me most. Functions I am grasping. Writing
cycles not so much.
int main(void)
{
double a[] = 2.2, 4.2, 5.00;
^

You are initializing an array. In C you can do that with curly
backets {}:
double a[] = { 2.2, 4.2, 5.00 };

mean(a, 3);
^

In this case you are passing the number of elements of a[] array. I
suggest to use a syntax like the following:

mean(a, sizeof(a) / sizeof(a[0]));

What's the sizeof(a[0]) mean?

Again, you know this.

What does sizeof(int) mean?
What does sizeof(double) mean?
What does sizeof("string") mean?
or define a generic macro if you're going to use this syntax in
several points:

#define SIZEARRAY(a) (sizeof(a) / sizeof(a[0]))

In this way, you can modify the number of elements in the
initialization, but you haven't to modify the number of elements
that will be automatically calculated by the preprocessor.

Of course, this is valid only for static allocated array. You you
dinamically allocate an array, you are responsible to take its
lenght in some variable.

}
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

What's it trying to tell me?

See above.
 
G

Geoff

Le 28/05/11 21:28, Bill Cunningham a écrit :
I am trying here to calculate a mean of 3 values. I do not understand
the errors the compiler returned I've never seen them.

#include<stdio.h>

void mean(double *avg, int periods)
{
double val;
val = *avg / periods;
printf("%.2f\n", val);
}

int main(void)
{
double a[] = 2.2, 4.2, 5.00;
mean(a, 3);
}
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

What's it trying to tell me?

Bill

Thanks a lot Bill.

For unknown reasons (normally I ignore your threads) I typed your code
and compiled it with lcc-win.

NO ERRORS.

WHAT?

Yes, lcc-win issued NO ERRORS AT ALL.

After a lot of work I found that I introduced a bug by eliminating a
call to "error" when after an equals sign in an array initialization
I do not find an opening brace.

Why did I do that?

Forgot, this bug is there since at least 3-4 years...

Well, thanks for your questions.

jacob


I think I just spotted someone who doesn't use regression tests and
who doesn't have a standard set of pass/fail tests for his compiler.
 
K

Keith Thompson

Geoff said:
Le 28/05/11 21:28, Bill Cunningham a écrit : [...]
double a[] = 2.2, 4.2, 5.00;
[...]

Thanks a lot Bill.

For unknown reasons (normally I ignore your threads) I typed your code
and compiled it with lcc-win.

NO ERRORS.

WHAT?

Yes, lcc-win issued NO ERRORS AT ALL.

After a lot of work I found that I introduced a bug by eliminating a
call to "error" when after an equals sign in an array initialization
I do not find an opening brace.

Why did I do that?

Forgot, this bug is there since at least 3-4 years...

Well, thanks for your questions.


I think I just spotted someone who doesn't use regression tests and
who doesn't have a standard set of pass/fail tests for his compiler.

Or someone who didn't happen to have a regression test for that
particular case.

How big a regression test suite would you need to catch all possible C
coding errors?
 
G

Geoff

Geoff said:
Le 28/05/11 21:28, Bill Cunningham a écrit : [...]
double a[] = 2.2, 4.2, 5.00; [...]

Thanks a lot Bill.

For unknown reasons (normally I ignore your threads) I typed your code
and compiled it with lcc-win.

NO ERRORS.

WHAT?

Yes, lcc-win issued NO ERRORS AT ALL.

After a lot of work I found that I introduced a bug by eliminating a
call to "error" when after an equals sign in an array initialization
I do not find an opening brace.

Why did I do that?

Forgot, this bug is there since at least 3-4 years...

Well, thanks for your questions.


I think I just spotted someone who doesn't use regression tests and
who doesn't have a standard set of pass/fail tests for his compiler.

Or someone who didn't happen to have a regression test for that
particular case.
Interesting point. How long has the requirement for braces around
array initialization been in existence?
How big a regression test suite would you need to catch all possible C
coding errors?

As big as it takes to cover all cases for which your compiler emits
warnings or errors.
 
J

Joe Pfeiffer

Lew Pitcher said:
Bill, you've done it before. Why can't you write a "cycle" (aka a "loop")
this time?

I'm not Bill, but calling it a "cycle" threw me completely. I had no
idea what he was talking about.
 
S

Seebs

As big as it takes to cover all cases for which your compiler emits
warnings or errors.

That wouldn't be enough. You have to catch all possible circumstances
under which they could arise, such as "after every other possible thing
has happened" in case some prior event affects the outcome.

-s
 
G

Geoff

That wouldn't be enough. You have to catch all possible circumstances
under which they could arise, such as "after every other possible thing
has happened" in case some prior event affects the outcome.

-s

No, you only have to invoke a case that violates the rule that
triggers the error condition you are trying to test. This would have
caught the removal of the error call in question. That a compiler can
emit no errors and still be wrong is not in question. It's the testing
for errors for which the compiler is designed or REQUIRED to emit a
diagnostic that must be confirmed. That a compiler can emit secondary
errors related to or caused by the first error was proven in the OP.
 
P

Peter Nilsson

jacob navia said:
Le 28/05/11 21:28, Bill Cunningham a écrit :
     double a[] = 2.2, 4.2, 5.00; ....
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

...lcc-win issued NO ERRORS AT ALL.

Even if you claimed conformance, it's not required to. It
follows the context free grammar. The requirement of brace
enclosed aggregate initialisors is not within a constraint.
After a lot of work I found that I introduced a bug

QoI issue for sure, but I guess it just highlights that it isn't
a particularly common mistake made by programmers.

Still, might be interesting to see what lcc does in the case of:

typedef int array_t[2];
for (array_t a = 2, 0; a[0] < 4; a[0]++)
puts("Hello");
 
S

Seebs

No, you only have to invoke a case that violates the rule that
triggers the error condition you are trying to test.

I guess it depends on whether the problem is "this kind of error is never
caught no matter what" or only "there are circumstances where this error is
not caught".

-s
 
J

jacob navia

Le 30/05/11 06:04, Peter Nilsson a écrit :
jacob navia said:
Le 28/05/11 21:28, Bill Cunningham a écrit :
double a[] = 2.2, 4.2, 5.00; ...
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

...lcc-win issued NO ERRORS AT ALL.

Even if you claimed conformance, it's not required to. It
follows the context free grammar. The requirement of brace
enclosed aggregate initialisors is not within a constraint.
After a lot of work I found that I introduced a bug

QoI issue for sure, but I guess it just highlights that it isn't
a particularly common mistake made by programmers.

Still, might be interesting to see what lcc does in the case of:

typedef int array_t[2];
for (array_t a = 2, 0; a[0]< 4; a[0]++)
puts("Hello");
The code was correctly generated as if there was braces around the
expression...
 
J

jacob navia

Le 30/05/11 03:14, Geoff a écrit :
I think I just spotted someone who doesn't use regression tests and
who doesn't have a standard set of pass/fail tests for his compiler.

Sure, YOUR regression tests catch everything, you never make mistakes
and when you do, your sophisticated tests catch them all.


Well you are the best, far better than me.

Happy?
 
G

Geoff

Le 30/05/11 03:14, Geoff a écrit :


Sure, YOUR regression tests catch everything, you never make mistakes
and when you do, your sophisticated tests catch them all.

I never said my tests were perfect or sophisticated.

Far from it. All programs have bugs. All compilers and regression
tests are programs. Therefore all compilers and regression tests have
bugs.

It is the purpose of regression testing to catch bugs that were
introduced since the last time the test was run against the
application under development. No regression is perfect but if it was
designed to catch an error it ought to catch the same error on a
subsequent run. Unless the specification changed, in which case you
would be modifying the application and the test, hopefully
independently and using different algorithms for calculations where
needed.
 
T

Tim Rentsch

Peter Nilsson said:
jacob navia said:
Le 28/05/11 21:28, Bill Cunningham a @C3{A9}crit :
double a[] = 2.2, 4.2, 5.00; ...
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

...lcc-win issued NO ERRORS AT ALL.

Even if you claimed conformance, it's not required to. It
follows the context free grammar. [snip]

Sure looks like a syntax error to me. You have a
derivation to offer?
 
K

Keith Thompson

Geoff said:
I never said my tests were perfect or sophisticated.

Far from it. All programs have bugs. All compilers and regression
tests are programs. Therefore all compilers and regression tests have
bugs.

It is the purpose of regression testing to catch bugs that were
introduced since the last time the test was run against the
application under development. No regression is perfect but if it was
designed to catch an error it ought to catch the same error on a
subsequent run. Unless the specification changed, in which case you
would be modifying the application and the test, hopefully
independently and using different algorithms for calculations where
needed.

Ok, he had a bug in his regression test suite. Ideally, he should
have caught the particular error; he didn't.

It's absurd to conclude from that one data point that he "doesn't
use regression tests".

(I'm not sure why I'm defending jacob here; he's certainly not
going to appreciate it, and he'll probably accuse me of leading
the attack.)
 
A

Alan Curry

Peter Nilsson said:
jacob navia said:
Le 28/05/11 21:28, Bill Cunningham a @C3{A9}crit :
double a[] = 2.2, 4.2, 5.00; ...
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

...lcc-win issued NO ERRORS AT ALL.

Even if you claimed conformance, it's not required to. It
follows the context free grammar. [snip]

Sure looks like a syntax error to me. You have a
derivation to offer?

I also think it's a syntax error, but take a look at this more subtle case:

int i=1, j=2;

int main(void)
{
int a[] = i, j;
}

It's invalid because the array initializer doesn't have braces. But that's
not a requirement of the grammar. In n1256.pdf, the clause that it violates
is 6.7.8.16, under the heading of "Semantics", not "Constraints".

And it can be made valid by putting braces around just the i, which makes a
single-element a[] and a local j shadowing the global j:
int a[] = {i}, j;
or by putting braces around both, making a 2-element a[]:
int a[] = {i, j};

A compiler that accepts the form without braces must be choosing one of those
interpretations. Which one?
 
K

Keith Thompson

Peter Nilsson said:
jacob navia said:
Le 28/05/11 21:28, Bill Cunningham a écrit :
     double a[] = 2.2, 4.2, 5.00; ...
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

...lcc-win issued NO ERRORS AT ALL.

Even if you claimed conformance, it's not required to. It
follows the context free grammar. The requirement of brace
enclosed aggregate initialisors is not within a constraint.

It's a syntax error. An initializer is either an initializer-list
enclosed in curly braces (possibly followed by a final comma),
or an assignment-expression. ``2.2, 4.2, 5.00'' is not an
assignment-expression.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

average problem 13
pointer array problem? 7
How to fix this code? 1
avg 36
error 20
puzzling error 49
How can I view / open / render / display a pdf file with c code? 0
nodes 4

Members online

No members online now.

Forum statistics

Threads
474,091
Messages
2,570,605
Members
47,225
Latest member
DarrinWhit

Latest Threads

Top