鱼C论坛

 找回密码
 立即注册
查看: 4090|回复: 3

结构体

[复制链接]
发表于 2013-1-14 11:28:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. //结构体变量

  2. #include <stdio.h>
  3. int main(int argc, char *argv[])
  4. {
  5.         struct student
  6.         {
  7.                  char name[10];
  8.                  float score;
  9.          }student1={"lihua",88.8}, student2={"fishc",99.9};
  10.          
  11.          
  12.          printf("%s %f",student.name,student.score);
  13.         return 0;
  14. }

  15. /*

  16. [Error] G:\c\c-free\结构体1.cpp:13: error:
  17. expected primary-expression before '.' token
  18. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-14 12:18:02 | 显示全部楼层
输出的时候应该要指定要输出的结构体对象,比如student1.name,student1.score.
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-14 14:15:28 | 显示全部楼层
本帖最后由 思无邪 于 2013-1-14 14:22 编辑

嗯楼上的正确啊。
打个比喻吧:你声明一个int数组 int a[10]={1,2,3,4,5,6,7,8,9,10};
可是你输出的时候用的却是 int[2] 编译器当然不认识了。
不能把数据类型当成变量用哦!
struct student
{
       char name[10];
        float score;         
}student1;
struct student是数据类型, student1是struct student类型的一个变量。
如果在结构体定义以后再声明变量,请注意:
struct student  a,b,c,d;//不要忘了 struct

同时这个例子也说明了一个良好的编程习惯,能有效的防止错误发生。如果你把下面的student1改成一个有意义的变量名,比如说,zhangsan,用的时候就不会发生这样的错误。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-14 21:52:22 | 显示全部楼层
输出应该是
student1和student2
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-9 04:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表