integral constant expression

K

Keith

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
 
T

Tom St Denis

Keith said:
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
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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);
}
}
 
M

Micah Cowan

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

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

Yeah: Use malloc().

-Micah
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top