#include <iostream>
using namespace std;
struct test{
char charr[5];};
int main(){
test t{
.charr="ss"};
cout<<" "<<t.charr<<endl;
return 0;
}
//result
error: C99 designator 'charr' outside aggregate initializer
.charr="ss"};
#include <iostream>
#include <string.h>
using namespace std;
struct test{
char charr[5];};
int main(){
test t;
strcpy(t.charr,"ss");
cout<<" "<<t.charr<<endl;
return 0;
}
供参考:
#include <iostream>
using namespace std;
struct test{
char charr[5];};
int main(){
//test t{
// .charr="ss"};
struct test t = {"ss"};
cout<<" "<<t.charr<<endl;
system("pause");
return 0;
}
但当我将charr填满又不会有错误了,这是为什么,像这样
//...
test t{
.charr="ssss"};
//...