Quick C99 standards question

This is a discussion on Quick C99 standards question within the C Programming forums, part of the General Programming Boards category; Is this valid under the variable-length array standard in C99? Code: int foo(char *p) { char s[ strlen(p) ]; .......... ...

  1. #1
    .
    Join Date
    Nov 2003
    Posts
    307

    Quick C99 standards question

    Is this valid under the variable-length array standard in C99?

    Code:
    int foo(char *p)
    {
        char s[ strlen(p) ];
        ..........
    }
    And yes, it compiles with no warnings (gcc). I thought one needed to explicity use an int or size_t, etc. like this:
    Code:
    int foo(char *p, size_t x)
    {
        char s [ x ];
        ......
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Nope, it's valid. You can even use a function call!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by C99
    6.7.5.2 Array declarators

    Constraints

    1 In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. The element type shall not be an incomplete or function type. The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.
    Since strlen's return value, size_t, is an integral type I see no issue.
    Last edited by Dave_Sinkula; 08-22-2005 at 02:41 PM. Reason: Pokey, and apparently getting pokier. :(
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-27-2005, 12:05 AM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 04:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 05:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21