gpt4 book ai didi

C 结构示例,编译期间出错

转载 作者:太空宇宙 更新时间:2023-11-04 00:25:04 24 4
gpt4 key购买 nike

我正在尝试熟悉 C 中的 struct 和指针,但我遇到了一堆语法错误,例如 "missing ';'在类型之前“在类型之前缺少')'“未声明的标识符:'i'”。一切似乎都很好,我知道 i 已声明,而且我似乎没有遗漏任何 ;)

#include <stdlib.h>
#include <stdio.h>

#pragma warning(disable: 4996)
struct Room;
struct House;

struct Room
{
float width;
float length;
float height;
char *name;
};

struct House
{
char *address;
struct Room *rooms[10];
};

int main(int argc, char* argv[])
{

struct House h;
h.address = "10 Palace Road";
for(int i = 0; i < 10; i++) // 6 errors occur here
{
h.rooms[i] = NULL;
}
struct Room hall;
hall.width = 10;
hall.length = 12;
hall.height = 9;
hall.name = "Hall";

h.rooms[0] = &hall;
printHouse(h);
system("PAUSE");
return 0;

}

void printHouse(struct House house)
{

printf(house.address);
printf("\n\n\n");

for (int i=0; i<10; i++)
{
if (house.rooms[i] != NULL)
{
struct Room r = *house.rooms[i];
printf("Room # %d: %s", i+1, r.name);
}
}
}

最佳答案

printf(house.address);

应该是

printf("%s",house.address);

您还必须声明您的函数 printhouse,因为您已经在 main 之后定义了它。

#include <stdlib.h>
#include <stdio.h>
#pragma warning(disable: 4996)
struct Room; //you don't need this

**EDIT**
struct House
{
char *address;
struct Room *rooms[10];
};
void printHouse(struct House house);

先声明房子,再声明函数。

关于C 结构示例,编译期间出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21922722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com