比如我定义了一个结构happy
struct happy
{
char me[];
};
那么我该如何为这个字符数组赋值呢?
直接赋值好像行不通啊
可以在结构体声明时直接赋值,这样char me[]的中括号内不需要填,否则必须声明字符数组大小,否则就是没有大小的字符数组,当然不能放进去值
你想要用户自己输入可以用
struct MyStruct
{
char me[80];
};
#include <iostream>
using namespace std;
int main() {
MyStruct * my = new MyStruct();
char d;
for (int i = 0; i < 80; i++) {
cin.get(d);
my->me[i] = d;
if (d == '\n') {
break;
}
}
return 0;
}
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct happy
{
char me[10];
};
int main()
{
happy h;
strcpy(h.me, "hello");
printf("%s", h.me);
}
如果要用户输入
int main()
{
happy h;
scanf("%s", &h.me[0]);
printf("%s", h.me);
}