Find the size of an array
Richard Heathfield said:
I really wish gcc switches were as carefully planned out as the rest
of the compiler suite. They're a complete mess. For instance, any rational
being would expect -Wall to mean, well, ALL. Having an 'extra' alongside
'all' should be reserved for infomercials.
And I was typing "-ansi -pedantic" like a robot for years before I
figured out what the precise differences are between those two switches.
I suspect I may not be alone. If I am alone, maybe I won't be alone
for much longer if we toss in the dialect switches.
>
Variable-length arrays were introduced in C99. 'asize' is a variable.
To prove my point about the gcc switches, I invite CLC readers to
determine which switch can be removed to inhibit the warning that the
OP is receiving. No cheating now!
I, for one, am proud of how Richard is coming along. We're getting
a Palin-esque folksy tone now, not the usual stilted tone that has
characterized Richard all these years. I am happy that the treatment
is working.
I prefer the following:
char arrc[] = "URI";
char new_arr[sizeof arrc * sizeof arrc / (sizeof arrc[0] * sizeof arrc)];
Yours,
Han from China
Richard Heathfield said:
>arnuld said:
>
>
>
>
>
>I want to create a new array of the same size of an array already
>available:
>available:
>#include <stdio.h>
>#include <string.h>
>#include <string.h>
>int main(void)
>{
> char arrc[] = "URI";
> const int asize = sizeof(arrc) / sizeof (arrc[0]);
> char new_arr[asize];
>{
> char arrc[] = "URI";
> const int asize = sizeof(arrc) / sizeof (arrc[0]);
> char new_arr[asize];
> printf("new_arr size = %d\n", sizeof(new_arr) );
> return 0;
>}
>}
>============== ========== OUTPUT =============== =====
>[arnuld@dune C]$ gcc4 -ansi -pedantic -Wall -Wextra test.c
>[arnuld@dune C]$ gcc4 -ansi -pedantic -Wall -Wextra test.c
I really wish gcc switches were as carefully planned out as the rest
of the compiler suite. They're a complete mess. For instance, any rational
being would expect -Wall to mean, well, ALL. Having an 'extra' alongside
'all' should be reserved for infomercials.
And I was typing "-ansi -pedantic" like a robot for years before I
figured out what the precise differences are between those two switches.
I suspect I may not be alone. If I am alone, maybe I won't be alone
for much longer if we toss in the dialect switches.
>test.c: In function ?main?:
>test.c:9: warning: ISO C90 forbids variable-size array ?new_arr?
>[arnuld@dune C]$ ./a.out
>new_arr size = 4
>[arnuld@dune C]$
>test.c:9: warning: ISO C90 forbids variable-size array ?new_arr?
>[arnuld@dune C]$ ./a.out
>new_arr size = 4
>[arnuld@dune C]$
>The program gives correct answer. But Why the warning. sizeof() is
>compile time operator then why do I get some warning related to run-time
>?
>compile time operator then why do I get some warning related to run-time
>?
Variable-length arrays were introduced in C99. 'asize' is a variable.
To prove my point about the gcc switches, I invite CLC readers to
determine which switch can be removed to inhibit the warning that the
OP is receiving. No cheating now!
Yes,
>sizeof's result is evaluated by the compiler. But, by the time the
>compiler is looking at new_arr, it's forgotten all about the sizeof in the
>previous line. All it can see is that you've got this asize thing
>, which
>is a const int *but NOT an integral constant expression* - a puzzling
>distinction, but a very real one in C.
>
>You can get what you want, however, by cutting out the middle-man:
>sizeof's result is evaluated by the compiler. But, by the time the
>compiler is looking at new_arr, it's forgotten all about the sizeof in the
>previous line. All it can see is that you've got this asize thing
>, which
>is a const int *but NOT an integral constant expression* - a puzzling
>distinction, but a very real one in C.
>
>You can get what you want, however, by cutting out the middle-man:
I, for one, am proud of how Richard is coming along. We're getting
a Palin-esque folksy tone now, not the usual stilted tone that has
characterized Richard all these years. I am happy that the treatment
is working.
>char arrc[] = "URI";
>char new_arr[sizeof arrc / sizeof arrc[0]];
>char new_arr[sizeof arrc / sizeof arrc[0]];
I prefer the following:
char arrc[] = "URI";
char new_arr[sizeof arrc * sizeof arrc / (sizeof arrc[0] * sizeof arrc)];
Yours,
Han from China
Comment