2

I have a program where I get the error:

variable modified 'artikel' at file scope
char artikel[ARTNAME];

I think I have to add a #define, but exactly which one should it be?

Here is my code:

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

const int ARTNAME = 100;

typedef struct artikel {
    char artikel[ARTNAME];
    int anzahl;
} artikel;

int main()
{
}
8
  • 1
    #define ARTNAME 100. variably modified array at file scope in C Commented Dec 29, 2020 at 13:55
  • i add #define ARTNAME 100 and now it gives me out----> main.c:5:20: error: expected identifier or ‘(’ before numeric constant #define ARTNAME 100 Commented Dec 29, 2020 at 13:57
  • 1
    Cannot reproduce: godbolt.org/z/1Mo69q Commented Dec 29, 2020 at 13:58
  • 1
    @flavio, you do not add the #define. You change the existing declaration of ARTNAME into a #define. Commented Dec 29, 2020 at 14:08
  • 2
    Does this answer your question? Variably modified array at file scope in C Commented Jul 29, 2023 at 12:08

1 Answer 1

2

I have a program where I get the error: variable modified 'artikel'at file scope char artikel[ARTNAME];

I don't think you have copied the diagnostic accurately. It is surely complaining about struct artikel being variably modified.

That is closely related to the issue covered in Variably modified array at file scope in C. Specifically, const qualification notwithstanding, ARTNAME is not a "constant expression" in C, so the type of array artikel.artikel and therefore also of the whole struct artikel are "variably modified". Objects with variably modified types cannot be declared at file scope.

I think I have to add a #define, but I don't understand exactly which one.

The quickest solution would probably be to change this ...

    const int ARTNAME = 100;

... to:

#define ARTNAME 100
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.