1

I'm trying to create a new String with a certain number of characters. I'm getting an error that says "error: variable length array used" and I'm not sure why. Here is my code:

int totalSize = getTotalSize(argv[1]); // gets the number of relevant chars
char totalString[totalSize]; // here is the error 

I'm guessing this has to do with malloc? What do I need to do? Thank you for your help!

Edit: I'm using a method that gets the total size so I won't be able to use macros. This is all done inside my main method.

13
  • Don't use a variable for the array length. Commented Feb 23, 2020 at 21:52
  • I see - but I want to create a string of fixed length and fill them with characters. How do I do this without using a variable for my initial array? Commented Feb 23, 2020 at 21:53
  • 1
    This is valid C99 code. Which compiler are you using and what was your exact compile command line? Commented Feb 23, 2020 at 21:55
  • 2
    If you can't use a VLA, you'll need to use dynamic memory allocation instead — malloc() et all. Commented Feb 23, 2020 at 22:06
  • 1
    You've changed the code in your question. My answer was valid for your original question (with int totalSize = 8;) but is no longer applicable. Commented Feb 23, 2020 at 22:24

1 Answer 1

4

UPDATE The question was edited after I posted this answer. In its current form, the size of the array is not known at compile time. This requires either using a VLA (and figuring out where that error message is coming from), using malloc, or allocating a constant-size array that's big enough for any expected use and using only the initial portion of it. The following applies to the question as it was originally written.

C90 required the length of an array to be a constant expression (something that can be evaluated at compile time). C99 introduced variable length arrays. C11 made VLAs optional.

Your code is invalid in C90, valid in C99, and conditionally valid in C11.

But given what you've shown us, you don't need a variable length array.

You have:

int totalSize = 8; 
char totalString[totalSize];

totalSize is not a constant. (And defining it as const wouldn't make it a constant, for complicated reasons I won't go into.) But the size of your array is always going to be 8.

You can just write:

char totalString[8];

but that's poor style. It can make it difficult to determine just what you have to modify if your requirements change.

You can do this:

#define TOTALSIZE 8
char totalString[TOTALSIZE];

(The convention is to use all-caps for macro names.)

Or you can play this trick with an enum to create a named constant of type int:

enum { TotalSize = 8 };
char totalString[totalSize];

Or, possibly, you can keep your code as it is (I'd add a const to the definition of totalSize if its value should never change) and figure out why you're getting that diagnostic message. You haven't told us what compiler you're using, or whether the message is from the compiler or some other tool. You certainly don't need a VLA in this context, but you might still be able to use one.

Sign up to request clarification or add additional context in comments.

5 Comments

In C++, constexpr would be even better than a preprocessor macro.
@ProXicT True. For that matter, in C++ const int totalSize = 8; would make totalSize a constant expression. That's one of the complexities I didn't want to address.
I see, it's just that many people consider preprocessor macros (in contexts like this) a code-smell in C++.
@ProXicT The question was tagged C, not C++.
C is C and C++ is a different computer language. Please If a question is tagged C comments and answers regarding C++ language syntax are simply out of place. Thks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.