setting to zero

K

Keith Thompson

Bill Cunningham said:
snip]
Every element of buf that you have defined is zero. Thus, adding them
to sum should always produce zero.

int buf[128]={0}; Make 128 ints equal 0 ? Making sure.
Yes.
Why do you expect sum to ever have a value other than zero? Why is sum
the loop control for the iteration, rather than i?

I don't think I'm sure of the engineering done here.

I'm not sure I'd call it engineering, and I'm not sure how to help
other than by just showing you the correct code.
This looks exactly like the usual idiom for summing the members of an
array, except that instead of counting until your loop counter reaches
the end of the array ("i < 128"), you count until the sum reaches a
particular value. Why?

That last line under for always confuses me. If int buf[128]={0}; works
for all elements that should be all I need.

The declaration of buf is just fine. It's what you (try to) do with
it later that's biting you.
 
A

Andrew Poelstra

[snip]
He's using gcc, which as we know is not a fully conforming C99
implementation, but it does support declarations in for loop headers
if you give it the right command-line arguments.

Oh great. I decided to change to 128 bytes. I don't know that I have
learned anything. Except things need to be initialized. I get a big number
with this too.

#include <stdio.h>

int main()
{
int buf[128] = { 0 };
int i, sum = 0;
for (i = 0; sum < 128; ++i)

You probably meant to test "i < 128", not "sum < 128". Others have mentioned
this - it looks to me to be a simple typo.

One thing you could also do is test for (sizeof buff / sizeof buff[0]) instead
of 128. This is a little advanced, but doesn't break if you change the size
of buf.
sum = sum + buf;
printf("%i\n", buf[128]);


The 128 ints defined by "int buf[128]" are buf[0] through buf[127]. Your
reference to buf[128] isn't valid, and is undoubtedly why you're getting
a strange number. (On many systems buf[128] might by i, which is probably
pretty big due to your botched loop above; on others, it might just crash
your program, and on others you'll get a completely random value.)

This also looks like a typo - perhaps you meant to print the value of
sum instead of anything in buf?
 
L

lovecreatesbeauty

Yes, in the context: "the same as objects that have static storage
duration".

Thanks, I didn't read and understand that paragraph very well.

Last time, it was 2005 when I wrote this kind of initialization for
character arrays `= {'\0'}' for huawei.com in their switch products.
Some leader and QA guys said "what's this? this works on vc6 or other
platforms, ehh?" and added corresponding memset() calls to initialize
the arrays there.

I read books about this construct, but didn't read them carefully and
understand them well. I couldn't show them the evidence. Fortunately,
I get confirmed on this group yesterday, it's years later after that.
Thanks dear!
 
S

Seebs

int buf[128]={0}; Make 128 ints equal 0 ? Making sure.
Yes.
Why do you expect sum to ever have a value other than zero? Why is sum
the loop control for the iteration, rather than i?
I don't think I'm sure of the engineering done here.
That last line under for always confuses me. If int buf[128]={0}; works
for all elements that should be all I need.

It does work for all elements, but your loop is still crazy.

Conventionally:

for (i = 0; i < 128; ++i)
sum = sum + buf;

your loop:

for (i = 0; sum < 128; ++i)
sum = sum + buf;

This makes no sense; the loop condition is unrelated to the loop
initialization and iteration expressions.

-s
 
B

Barry Schwarz

Yes probably. Sorry. I am confusing you with Barry.

If you are going to insult me, at least spell my name correctly.

And for the record, I never tried to trick you or suggest you use C99,
especially since I don't have a C99 compiler and wouldn't be able to
check anyone who did. But every time I asked you a question, you
answered a different one so I gave up.
 
P

Phil Carmody

Richard Heathfield said:
Parochialism rules okay! :)

The compiler I use on my POWER-family machine supports most of C99.
The compiler I use on my 386-family machine supports most of C99.
The compiler I use on my amd64-family machine supports most of C99.
The compiler I use on my arm-family machine supports most of C99.
The compiler I use on a friend's Sparc-family machine supports most of C99.
I no longer use my Alpha-family machine, but it supported most of C99.
I no longer use a HPPA-machine, but it supported most of C99.

And by 'use', I do mean almost daily.

Phil
 
K

Keith Thompson

Richard Heathfield said:
C99.

One problem with that is that, for each X, it's likely to be a
slightly different subset of C99 that is supported. This makes
portability problematical. If that's not an issue for you, that's
obviously fine.

If you're using the same compiler (or at least the same front end) for
each X, it might be less of an issue than you'd think.
 
K

Keith Thompson

Richard Heathfield said:
Right. And if you are using only one platform and one compiler that
happens to be by Edison, with a Dinkumware library to boot, it's no
problem at all, and is portable to all the platform you care about.
That doesn't affect my point in the slightest.

I disagree. I think it does affect your point in the slightest.
 
B

Bill Cunningham

If you are going to insult me, at least spell my name correctly.

And for the record, I never tried to trick you or suggest you use C99,
especially since I don't have a C99 compiler and wouldn't be able to
check anyone who did. But every time I asked you a question, you
answered a different one so I gave up.

Ok Barry then I'll leave it at that.

Bill
 
K

Keith Thompson

Richard Heathfield said:
I disagree, obviously, and somehow I don't think we're going to see
eye to eye on this.

I hope my repetition of the phrase "in the slightest" was clear.
I don't think we're very far apart, if at all.
 
P

Phil Carmody

Richard Heathfield said:
Restoring:
+>> I just assume that most compilers, by now, support mixed
+>> declarations and code and C99/C++ style for loops.
C99.

One problem with that is that, for each X, it's likely to be a
slightly different subset of C99 that is supported. This makes
portability problematical. If that's not an issue for you, that's
obviously fine.

They all support mixed declarations and code, and C++-style for loops.
I consider those, // comments, and a few other constructs to be so
common that they are not a portability issue.

Phil
 
C

Chris M. Thomasson

Bill Cunningham said:
I should know how to do this. I've done it before but I'm just not
getting the right result. I'm getting a huge number when it should be
zero.

#include <stdio.h>
#define __STRICT_ANSI__

int main(void)
{
int i[4], j, k;
k = 0;
for (j = 0; k < 4; ++j)
k = k + i[j];
printf("%d\n", k);
}

I'm not getting any errors and it compiles fine. Just not the right
result.

With regard to explicitly setting to zero... Well, let me go ahead and write
about a 100% standard technique of setting each member of an array of
anything to a constant that is NOT zero:

http://groups.google.com/group/comp.programming.threads/browse_frm/thread/d6f26d54c36d4783

;^)
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top