C和C++中的结构是不同的

C和C++中的结构是不同的

C语言中,定义结构的时候,结构内部一定是变量列表,不能包含方法,如下所示:

#include <stdio.h>

struct date{
int year;
int month;
int day;
};
struct student{
int number;
char name[20];
char gender;
struct date birthday;
char* description;
char* display(){
   return "hello!!!\n";
};
};

int main(){
student stu = {10001,"Shirdrn",'M',{1983,1,19},""};
stu.description = "Love my own love,never mind.";

printf("number : %d\n",stu.number);
printf("name : %s\n",stu.name);
printf("gender : %c\n",stu.gender);
printf("birthday-year : %d\n",stu.birthday.year);
printf("birthday-month : %d\n",stu.birthday.month);
printf("birthday-day : %d\n",stu.birthday.day);
printf("description : %s\n",stu.description);
printf(stu.display());
return 0;
}

这时,如果保存为.c文件,在C语言中编译会出错的:

--------------------Configuration: Test - Win32 Debug--------------------
Compiling...
STRUCT.C
d:\program files\microsoft visual studio\myprojects\test\struct.c(14) : error C2032: '<Unknown>' : function cannot be member of struct 'student'
d:\program files\microsoft visual studio\myprojects\test\struct.c(14) : error C2143: syntax error : missing ';' before '{'
d:\program files\microsoft visual studio\myprojects\test\struct.c(14) : error C2059: syntax error : '{'
d:\program files\microsoft visual studio\myprojects\test\struct.c(17) : error C2059: syntax error : '}'
d:\program files\microsoft visual studio\myprojects\test\struct.c(20) : error C2065: 'student' : undeclared identifier
d:\program files\microsoft visual studio\myprojects\test\struct.c(20) : error C2146: syntax error : missing ';' before identifier 'stu'
d:\program files\microsoft visual studio\myprojects\test\struct.c(20) : error C2065: 'stu' : undeclared identifier
d:\program files\microsoft visual studio\myprojects\test\struct.c(20) : error C2059: syntax error : '{'
d:\program files\microsoft visual studio\myprojects\test\struct.c(21) : error C2224: left of '.description' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(23) : error C2224: left of '.number' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(24) : error C2224: left of '.name' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(25) : error C2224: left of '.gender' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(26) : error C2224: left of '.birthday' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(27) : error C2224: left of '.birthday' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(28) : error C2224: left of '.birthday' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(29) : error C2224: left of '.description' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(30) : error C2224: left of '.display' must have struct/union type
d:\program files\microsoft visual studio\myprojects\test\struct.c(30) : error C2198: 'printf' : too few actual parameters
STRUCTCPP.cpp
Error executing cl.exe.

Test.exe - 18 error(s), 0 warning(s)

如果在C++中,上面的程序完全可以编译运行成功:

--------------------Configuration: Test - Win32 Debug--------------------
Compiling...
STRUCT.C
STRUCTCPP.cpp
Linking...

Test.exe - 0 error(s), 0 warning(s)

运行结果:

number : 10001
name : Shirdrn
gender : M
birthday-year : 1983
birthday-month : 1
birthday-day : 19
description : Love my own love,never mind.
hello!!!
Press any key to continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值