(part 11) Han from China answers your C questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Borked Pseudo Mailed

    (part 11) Han from China answers your C questions

    Find the size of an array

    Richard Heathfield said:
    >arnuld said:
    >I want to create a new array of the same size of an array already
    >available:
    >
    >#include <stdio.h>
    >#include <string.h>
    >
    >int main(void)
    >{
    > 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

    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]$
    >
    >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
    >?

    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:

    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]];

    I prefer the following:

    char arrc[] = "URI";
    char new_arr[sizeof arrc * sizeof arrc / (sizeof arrc[0] * sizeof arrc)];


    Yours,
    Han from China

  • viza

    #2
    Re: (part 11) Han from China answers your C questions

    On Thu, 06 Nov 2008 06:06:09 -0700, Borked Pseudo Mailed wrote:
    >>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.
    You aren't the first person to comment on how absolutely stupid that is.

    Comment

    • Kenny McCormack

      #3
      Re: (part 11) Han from China answers your C questions

      In article <b5DQk.1589$AS2 .1488@newsfe20. ams2>,
      viza <tom.viza@gm-il.com.obviousc hange.invalidwr ote:
      >On Thu, 06 Nov 2008 06:06:09 -0700, Borked Pseudo Mailed wrote:
      >
      >>>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.
      >
      >You aren't the first person to comment on how absolutely stupid that is.
      (I assume there is some historical/compatibility reason why it is the
      way it is)

      Maybe they need a new switch "-WEverything"...

      Comment

      Working...