吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C中结构变量的操作

C中结构变量的操作

来源:网络 | 更新时间:2022-01-21 12:29:23
在 C 中,唯一可以应用于结构变量的操作是赋值。不允许对结构变量进行任何其他操作(例如相等检查)。 例如,程序 1 运行时没有任何错误,而程序 2 编译失败。 程序 1
#include <stdio.h>

struct Point {
int x;
int y;
};

int main()
{
struct Point p1 = {10, 20};
struct Point p2 = p1; // works: contents of p1 are copied to p2
printf(" p2.x = %d, p2.y = %d", p2.x, p2.y);
getchar();
return 0;
}
程序 2
#include <stdio.h>

struct Point {
int x;
int y;
};

int main()
{
struct Point p1 = {10, 20};
struct Point p2 = p1; // works: contents of p1 are copied to p2
if (p1 == p2) // compiler error: cannot do equality check for 
// whole structures
{
printf("p1 and p2 are same ");
}
getchar();
return 0;
}
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com