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
Ben Bacarisse said:I don't know why you think I am trying to trick you.
Same as static != only static.
^^^^^^
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.
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.
No, because there is an initializer, therefore, the rest of it is initialized
THE SAME AS it would be if it had been static.
You do? Really? I have three or four within easy reach that don't.
You do? Really? I have three or four within easy reach that don't.
> ^^^^^^
> ^^^^^^
> They all mentioned `static'.
> 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.
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.
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.
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.
char a[4]={"0"};
The above doesn't work.
Beej Jorgensen said:That's one nasty-sounding drug; I hope you can do without it.
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.
Seebs said:char a[4]={"0"};
The above doesn't work.
Because a[0] is not of type (char *).
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?
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.
[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 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)
sum = sum + buf;
printf("%i\n", buf[128]);
return 0;
}
It's 8 or 9 digits long. What did I do here wrong?
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?
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?
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.