integral constant expression

Discussion in 'C Programming' started by Keith, Jul 12, 2003.

  1. Keith

    Keith Guest

    I receive compiler error messge: "integral constant expression expected"
    for:

    somefunc(char *bar)
    {
    char foo[2*strlen(bar)];
    }


    Is there any way around this? I am using SunWorkStop 5.0.
    Thanks
    Keith, Jul 12, 2003
    #1
    1. Advertising

  2. Keith

    Tom St Denis Guest

    Keith wrote:
    > I receive compiler error messge: "integral constant expression expected"
    > for:
    >
    > somefunc(char *bar)
    > {
    > char foo[2*strlen(bar)];
    > }
    >
    >
    > Is there any way around this? I am using SunWorkStop 5.0.


    That's called a variable length array [VLA] and you need an ISO C [c99]
    compiler to handle that.

    Short of that you may want to look into alloca [not portable] or malloc
    [portable].

    Tom
    Tom St Denis, Jul 12, 2003
    #2
    1. Advertising

  3. In 'comp.lang.c', Keith <> wrote:

    > I receive compiler error messge: "integral constant expression expected"
    > for:
    >
    > somefunc(char *bar)
    > {
    > char foo[2*strlen(bar)];
    > }
    >
    > Is there any way around this? I am using SunWorkStop 5.0.


    #include <stdlib.h>

    somefunc(char *bar)
    {
    char *foo = malloc (2 * strlen(bar));
    /* + 1 if you need room for the final 0. */

    if (foo != NULL)
    {
    /* use it ... */


    /* ... and when finished : */
    free (foo);
    }
    }

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jul 12, 2003
    #3
  4. Keith

    Micah Cowan Guest

    Keith <> writes:

    > I receive compiler error messge: "integral constant expression
    > expected" for:
    >
    > somefunc(char *bar)
    > {
    > char foo[2*strlen(bar)];
    > }


    Yeah: Use malloc().

    -Micah
    Micah Cowan, Jul 14, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tomás

    A constant integral

    Tomás, Mar 3, 2006, in forum: C++
    Replies:
    1
    Views:
    320
    Victor Bazarov
    Mar 3, 2006
  2. _Christopher\(M2M\)
    Replies:
    3
    Views:
    2,086
    _Christopher\(M2M\)
    Jun 8, 2007
  3. Replies:
    14
    Views:
    1,160
    Ian Collins
    Mar 18, 2008
  4. Adem
    Replies:
    42
    Views:
    2,770
    James Kuyper
    Nov 4, 2008
  5. Adem
    Replies:
    45
    Views:
    1,734
    James Kuyper
    Nov 4, 2008
Loading...

Share This Page