求帮助

黑刀 2012-11-22 08:32:20
#include <stdio.h>

struct point
{
struct point1
{
int x;
int y;
};

struct point2
{
int x;
int y;
};
};

int main()
{
struct point point1 pt1;
//struct point rect
}

C语言中可以这样为嵌套内的结构体申请变量吗?
如果不可以请大牛们介绍下方法 小弟借鉴一下~
...全文
72 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
prajna 2012-11-22
  • 打赏
  • 举报
回复
看下面的説明:

struct point
{
	struct point1
	{
		int x;
		int y;
	} p1; //加入struct point的 域名 (c++叫成員變量名)

	struct point2
	{
		int x;
		int y;
	} p2;
};

int main()
{

	//struct point point1 pt1; //這個是錯的
	struct point2 pt2; // gcc 可以

	//這樣寫可以
	struct point pt;	
	return 0;
}
黑刀 2012-11-22
  • 打赏
  • 举报
回复
引用 5 楼 anhuizhuanjiao 的回复:
引用 4 楼 Black_Knife 的回复: 补充一下 在main()函数中申请内嵌套结构变量 而不是struct{}xxx;这样 不可以的,可以像下面这样 C/C++ code? 123456789101112131415161718192021222324252627282930 struct point { struct point1 ……
谢谢 我知道啦~
转角天边 2012-11-22
  • 打赏
  • 举报
回复
引用 4 楼 Black_Knife 的回复:
补充一下 在main()函数中申请内嵌套结构变量 而不是struct{}xxx;这样
不可以的,可以像下面这样


struct point
{
	struct point1
	{
		int x;
		int y;
	};

	struct point2
	{
		int x;
		int y;
	};

	point1  a;
	point2  b;
};

int main()
{
	struct point p;
	p.a.x=1;
	p.b.y=2;
	//struct point rect

	system("pause");
	return 0;
}
黑刀 2012-11-22
  • 打赏
  • 举报
回复
补充一下 在main()函数中申请内嵌套结构变量 而不是struct{}xxx;这样
breakfisher 2012-11-22
  • 打赏
  • 举报
回复
#include <stdio.h>

typedef struct point1
{
  int x;
  int y;
}point1;

typedef struct point
{
  point1 p1;
  point1 p2;
}point;

int main()
{
  point pt; 
  point1 pt1;
  //struct point rect
}
刚刚copy错了,参考这个
breakfisher 2012-11-22
  • 打赏
  • 举报
回复
#include <stdio.h>

struct point
{
struct point1
{
int x;
int y;
};

struct point2
{
int x;
int y;
};
};

int main()
{
struct point point1 pt1;
//struct point rect
}
参考这个代码用吧
newtee 2012-11-22
  • 打赏
  • 举报
回复
struct point { int x; int y; struct point *next; };

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧