8

Here's the simple example program

#include <stdio.h>
#include <string.h>

const char *hello_string = "Hello";

int main(void)
{
char *world_string = " World";
static char hello_world[strlen(hello_string)+strlen(world_string)];
strcpy(&hello_world[0], hello_string);
strcat(&hello_world[0], world_string);
printf("%s\n", hello_world);
return 0;
}

The compiler output:

test.c: In function ‘main’:
test.c:9:13: error: storage size of ‘hello_world’ isn’t constant
static char hello_world[strlen(hello_string)+strlen(world_string)];
            ^

I realize the completely useless and unnecessary use of "static" in this case causes the error and with its removal things will compile fine. This is just a simple example to illustrate my question.

What I don't understand is why the storage size is not a constant when the "hello_string" is declared as const char * and its size is not going to change during the course of execution. Is this just a case of the compiler not being smart enough to know that?

user2738091
  • 93
  • 1
  • 1
  • 3
  • Duplicate, see answer [here](http://stackoverflow.com/questions/10675399/why-cant-the-size-of-a-static-array-be-made-variable) – Scotty Bauer Sep 01 '13 at 21:09

4 Answers4

5

When the compiler complains about storage size not being constant implicitly means compile-time constant, i.e. a value that the compiler can determine at compile-time. The call of strlen obviously would happen at runtime, so the compiler cannot know the size of your array.

Try this:

#include <stdio.h>
#include <string.h>

const char hello_string[] = "Hello";

int main(void)
{
    char world_string[] = " World";
    static char hello_world[sizeof hello_string + sizeof world_string];
    strcpy(&hello_world[0], hello_string);
    strcat(&hello_world[0], world_string);
    printf("%s\n", hello_world);
    return 0;
}
4

strlen is a function. It's return value cannot be calculated at compile time.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Why couldn't the value of `strlen("Hello")` be computed at compile time? It certainly could and moreover a compiler like `gcc` will even actually do this optimization. The problem is C requires here an integer constant expression and the use of a function call disqualifies the expression to be considered constant with the rules of C. – ouah Sep 01 '13 at 22:04
  • @ouah Because `strlen` is a function. Depending on how you link the object files, there's no guarantee that it's a function that actually computes the length of the string. The compiler is not allowed to infer that. – JeremyP Sep 02 '13 at 07:01
  • Then why this code have same error? `static const uint16_t width = 480;static uint16_t tdl[width]={0};` – mohammadsdtmnd Jan 17 '22 at 06:02
2

You used the static storage class speficier in your array declaration.

static arrays can only have fixed length: i.e., the size of the array has to be an integer constant expression. An expression involving a function call is not a constant expression.

Remove the static specifier if you want to use a variable length array. And then don't forget to reserve an extra character in your array for the null terminator.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

strlen() is a function call. The compiler doesn't know what it does.

Try sizeof (*hello_string). I'm not sure whether that would work.

Or const char hello_string[] = "Hello" and sizeof(hello_string), which seems to me more likely to work.

Arlie Stephens
  • 1,146
  • 6
  • 20