2

Consider:

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

const int STRING_SIZE = 40;
typedef char string[STRING_SIZE];

char typeInput(string message);

int main()
{
    typeInput("Hi my name is Sean");
}

char typeInput(string message)
{
    printf(" %s", message);
}

Error:

error: variably modified 'string' at file scope

I keep getting this error for some reason. Where did I go wrong?

Just in case, I'm using Code::Blocks.

7
  • I named the char variable "string" Commented Oct 22, 2016 at 16:57
  • Wait what do you mean (noob programmer) Commented Oct 22, 2016 at 17:01
  • const int STRING_SIZE = 40; --> #define STRING_SIZE 40. Also Change type of return of typeInput to void from char. Commented Oct 22, 2016 at 19:02
  • stackoverflow.com/questions/1712592/… Commented Oct 22, 2016 at 22:32
  • @melpomene: No, that is for Objective-C, not C Commented Jul 29, 2023 at 11:28

1 Answer 1

4

In C, const doesn't declare a constant, it declares a read-only variable. The compiler complains because STRING_SIZE is not a constant.

Workaround:

enum { STRING_SIZE = 40 };
// enum creates constants in C (but only integer ones)
Sign up to request clarification or add additional context in comments.

Comments

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.