0

FYI, I'm new to programming. When I try to compile this code it gives error saying:

[Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
[Error] initializer-string for array of chars is too long [-fpermissive]

I have searched a lot but all of the articles use this exact method. Can anyone explain why am I getting this error? Explain in language I can understand. I didn't understand most of the answers and reasons stated.

#include <stdio.h>
struct person{
    char name[] = "something";  
};
int main(){
    struct person per;
    printf("%s",per.name);
}
Anish
  • 19
  • 1
  • 8

1 Answers1

1

Simply - because C language does not allow it as C does not have constructors. You need to initialize the variables yourself when you define them.

#include <stdio.h>
struct person{
    char name[100];  
};
int main(){
    struct person per = {.name = "something"};
    printf("%s",per.name);
}
0___________
  • 60,014
  • 4
  • 34
  • 74