#define NAMESIZE 20
struct point {
int x;
int y;
};
struct rectangle {
struct point upperleft;
struct point lowerright;
char label[NAMESIZE + 1];
};
在我的主要
int main(void) {
struct rectangle r1;
r1.upperleft.x = 1;
r1.upperleft.y = 4;
r1.lowerright.x = 4;
r1.lowerright.y = 1;
strcpy_s(r1.label, NAMESIZE, "hi");
printf("Upper left: (%d, %d)\n", r1.upperleft.x, r1.upperleft.y);
printf("Lower Right: (%d, %d)\n", r1.lowerright.x, r1.lowerright.y);
printf("Name of rectangle: %d", r1.label);
return 0;
}
我的矩形名称是一个内存地址,我应该如何正确初始化我的结构的字符变量?
答案 0 :(得分:1)
struct rectangle r1 = {{1,4}, {4,1}, "hi"};
printf("Upper left: (%d, %d)\n", r1.upperleft.x, r1.upperleft.y);
printf("Lower Right: (%d, %d)\n", r1.lowerright.x, r1.lowerright.y);
printf("Name of rectangle: %s", r1.label);
答案 1 :(得分:0)
这将清除它:memset(&r1.label, 0, sizeof(r1.label))
这是对您所拥有的内容的更正:strcpy_s(&r1.label, NAMESIZE+1, "hi");
&
返回“地址”。因此&r1.label
是r1中标签的地址。