setting to zero

B

Bill Cunningham

Ben Bacarisse said:
I don't know why you think I am trying to trick you.

You know that's c99. I use c89. You know if I tried what you intended my
compiler would hang.

Bill
 
L

lovecreatesbeauty

Same as static != only static.

Thanks.

I didn't pay enough attention to `..same as..'. Pardon me for the
boring posts, me not a native English speaker!
 
S

Seebs

^^^^^^
They all mentioned `static'.

Yes, they did.

They said THE SAME AS objects that have static storage duration.

What they mean is that automatic objects under these cirumstances receive
THE SAME initialization as static ones.
I can understand that three rest of `a'
and `b' are zero. Array `i' is an auto variable, three rest of `i' are
indeterminate.

No, because there is an initializer, therefore, the rest of it is initialized
THE SAME AS it would be if it had been static.

-s
 
L

lovecreatesbeauty

Yes, they did.

They said THE SAME AS objects that have static storage duration.

What they mean is that automatic objects under these cirumstances receive
THE SAME initialization as static ones.

Thank you.
No, because there is an initializer, therefore, the rest of it is initialized
THE SAME AS it would be if it had been static.

I eventually understand it, the code and 6.7.8p19, p21 now, sorry for
my boring posts. Thanks for help me a non native-english speaker c
programmer. (PS. Jacob Navia isn't a native English speaker, but his
English is very good. I admire his language skill.)
 
S

Seebs

You do? Really? I have three or four within easy reach that don't.

$dayjob is Linux these days. What that means is that in practice, "most
compilers" means "both gcc 4.1 and gcc 4.3". I haven't had reason to use
any compilers which didn't support the big obvious C99 language changes
in a while now; even before I was here, I was mostly using BSD, with the
same thing applying; I could assume that I had C99 features available,
in general.

-s
 
D

Dik T. Winter

> ^^^^^^
> ^^^^^^
> They all mentioned `static'.

Yes, in the context: "the same as objects that have static storage
duration".
> They all mentioned `static'. I can understand that three rest of `a'
> and `b' are zero. Array `i' is an auto variable, three rest of `i' are
> indeterminate.

Nope, because i is partially intialised, it is initiased the same as an
object that has static duration.
 
B

Bill Cunningham

No I don't (because it won't). Anyway, I now know you use only C89.
I am still baffled by why you'd assume I am trying to trick you.

I thought you meant that I was so stupid that I wouldn't know what a
compiler error was or that declaring and initializing an int in a for
parameter space was an error. It's not that I don't like the idea of c99 but
most everything is still c89. Maybe I'm getting you confused with Barry
Schwartz.

Bill
 
B

Bill Cunningham

Bill Cunningham said:
I thought you meant that I was so stupid that I wouldn't know what a
compiler error was or that declaring and initializing an int in a for
parameter space was an error. It's not that I don't like the idea of c99
but most everything is still c89. Maybe I'm getting you confused with
Barry Schwartz.

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

Bill
 
B

Bill Cunningham

Keith this brings up another question I slept on all night. What about
type char?

char a[4]={'0'};
Would that be correct because it's a char I tried to initialize like a
string, try to use english instead of C I guess.

char a[4]={"0"};

The above doesn't work.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Keith this brings up another question I slept on all night. What about
type char?

char a[4]={'0'};
Would that be correct because it's a char I tried to initialize like a
string, try to use english instead of C I guess.

Yes, that works; it sets a (that's a bad name BTW) to
{'0', 0, 0, 0}

Note that the first byte hold the digit '0' (value 48 on ASCII-based
systems), and the other three bytes to 0, or '\0', the null character.
It's an array initializer that specifies a value only for the first
element of the array; unspecified elements are initialized to zero.
char a[4]={"0"};

The above doesn't work.

What do you mean it doesn't work? Did you get an error message;
if so, what was it? Did a end up with a value other than what
you expected?

In fact, the above works just fine for me, though it's written
rather oddly. Extra braces in an initializer are ignored. You could
even write:
int i = { 42 };
which is equivalent to
int i = 42;

You can also omit some braces in an initializer that you might assume
are necessary. For example:
int arr_2d[2][2] = { 10, 20, 30, 40 };
which would be more clearly written as:
int arr_2d[2][2] = { { 10, 20 }, { 30, 40 } };

C is more relaxed about braces in initializers than, in my humble
opinion, it should be, probably for historical reasons. You *should*
use braces to reflect the structure of the object you're initializing,
even if you can get away with omitting some of them. The exception is
when you're deliberately omitting initializers for some elements,
knowing that they'll be set to 0; for example this:
struct huge_nested_structure obj = { 0 };
is perfectly reasonable (though some compilers might warn about it).

Getting back to your example that you say "doesn't work":
char a[4]={"0"};
This is equivalent to:
char a[4] = "0";
And to:
char a[4] = { '0' };
And to:
char a[4] = { '0', '\0', '\0', '\0' };
Or:
char a[4] = { '0', 0, 0, 0 };
(noting again that '\0' == 0, but 0 != '0'.)

Of these, I prefer:
char a[4] = "0";
 
S

Seebs

Keith this brings up another question I slept on all night. What about
type char?

char a[4]={'0'};
Would that be correct because it's a char I tried to initialize like a
string, try to use english instead of C I guess.

That'd be fine. While a[4] could be initialized by a string literal, it's
an array, so you can also initialize it like an array literal.
char a[4]={"0"};

The above doesn't work.

Because a[0] is not of type (char *).

-s
 
K

Keith Thompson

Seebs said:
char a[4]={"0"};

The above doesn't work.

Because a[0] is not of type (char *).

But it *does* work, and the standard says so explicitly:

C99 6.7.8p14 (emphasis added):

An array of character type may be initialized by a character
string literal, *optionally enclosed in braces*. Successive
characters of the character string literal (including the
terminating null character if there is room or if the array is
of unknown size) initialize the elements of the array.

I personally dislike the permission to add extraneous braces,
but there it is.

Has anyone seen a compiler that rejects this?
 
S

Seebs

Because a[0] is not of type (char *).
But it *does* work, and the standard says so explicitly:

Whoops.

Right you are.
C99 6.7.8p14 (emphasis added):
An array of character type may be initialized by a character
string literal, *optionally enclosed in braces*. Successive
characters of the character string literal (including the
terminating null character if there is room or if the array is
of unknown size) initialize the elements of the array.
I personally dislike the permission to add extraneous braces,
but there it is.
Has anyone seen a compiler that rejects this?

I must have once, I think, but I couldn't think of one.

I'd assume it's for consistency; array initializers are in braces.
(But their teeth are almost straight, and the braces can come off
in a few weeks.)

-s
 
B

Bill Cunningham

[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)
sum = sum + buf;
printf("%i\n", buf[128]);
return 0;
}

It's 8 or 9 digits long. What did I do here wrong?

Bill
 
L

Lew Pitcher

[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)
sum = sum + buf;
printf("%i\n", buf[128]);
return 0;
}

It's 8 or 9 digits long. What did I do here wrong?



Where to begin?

You defined buf to contain 128 ints. buf[0] is the 1st int, buf[1] is the
second int, and so on. This means that buf[127] is the 128'th int that you
defined.

In your printf(), you access the contents of buf[128]. Notice that buf[128]
is /not/ part of the 128 ints that you defined buf to carry. Your buf[128]
is /something/ undefined. Thus, you got an unusual result (but it didn't
have to be; that's the way of "undefined").

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
K

Keith Thompson

Bill Cunningham said:
[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 terminate the loop when sum reaches or exceeds 128. I don't think
that's what you want. And since you initialized buf to all zeros, sum
will be 0 until you go *past* the end of buf; you'll then start adding
up garbage, unless your program crashes first.
sum = sum + buf;
printf("%i\n", buf[128]);


What do you expect buf[128] to be? Any answer is wrong, since
buf[128] doesn't exist.

You compute sum (though incorrectly), but you never do anything with
it.
return 0;
}

It's 8 or 9 digits long. What did I do here wrong?

Several things.
 
S

Seebs

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.

Yes, you do.
#include <stdio.h>
int main()
{
int buf[128] = { 0 };
int i, sum = 0;
for (i = 0; sum < 128; ++i)
sum = sum + buf;
printf("%i\n", buf[128]);
return 0;
}

It's 8 or 9 digits long. What did I do here wrong?

The same thing as before.

Every element of buf that you have defined is zero. Thus, adding them
to sum should always produce zero.

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?

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?

-s
 
B

Bill Cunningham

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.
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.
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.

Bill
 

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,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top