1

Yeah, so I’m new to C, and I came across an error when dealing with the following code:

typedef struct{
    int head;
    int length;
    Customer customer[MAX_LENGTH];

} CustomerCi;

And the error that came out was:

error: variably modified 'customer' at the file scope

How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3050546
  • 11
  • 1
  • 2
  • i tried those and some of them didnt work and others i didnt understand fully – user3050546 Nov 29 '13 at 20:08
  • They aren't good duplicate targets (and one of them is for Objective-C). The one for C is *[Variably modified array at file scope in C](https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c)*. – Peter Mortensen Jul 29 '23 at 09:12
  • See also [the linked questions](https://stackoverflow.com/questions/linked/13645936?sort=votes). – Peter Mortensen Jul 29 '23 at 12:16

1 Answers1

1

Replace MAX_LENGTH with a literal value:

#define MAX_LENGTH 32

for example.

ceving
  • 21,900
  • 13
  • 104
  • 178
  • yeah it works then but later on i need the value of MAX_LENGTH to be able to change. – user3050546 Nov 29 '13 at 20:28
  • the line before the ones i gave already defines MAX_LENGTH as "static const int MAX_LENGTH = 100;" – user3050546 Nov 29 '13 at 20:31
  • You can not change the size of an array in C. Either use pointers with malloc/free or use a [linked list](http://www.cprogramming.com/tutorial/c/lesson15.html). – ceving Nov 29 '13 at 20:35
  • oh ok because i was trying to use it to gauge the size of my queue – user3050546 Nov 29 '13 at 20:37
  • Use [Glib](https://developer.gnome.org/glib/2.38/glib-data-types.html) if you do not want to write it on your own. Glib has all you will ever need. – ceving Nov 29 '13 at 20:42
  • 2
    @user3050546: In C, `static const int MAX_LENGTH = 100;` is still regarded as a 'variable' and `MAX_LENGTH` cannot be used where a constant is required (for example, in case labels, or in array bounds at file scope). In C++, it would be usable like that. If you want C++, use it. But in C, you can't do what you tried. – Jonathan Leffler Nov 29 '13 at 22:53