工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1197|回复: 2

学过c语言的大虾帮帮忙咯

[复制链接]
发表于 2005-5-30 13:29 | 显示全部楼层 |阅读模式
我想由自定义函数输出结构数组,程序如下:
#include<stdio.h>
#define FORMAT "%-5d%-15s%4c%4d%4d%4d%4d\n"
#define N 5
struct student
{int num;
char name[15];
char sex;
int age;
int score[3];
}stu[N]={{1001,"Li Ming",'M',22,66,88,56},{1002,"WJian",'M',21,76,98,70},{1003,"Zhang Xiaoli",'F',23,78,68,60},{1004,"YU Xin",'M',20,87,100,80},{1005,"Zhao Fen",'F',21,86,76,72}};
void print(struct student stu[])
{int i;
for(i=0;i<N;i++)
  {printf(FORMAT,stu.num,stu.name,stu.sex,stu.age,
        stu.score[0],stu.score[1],stu.score[2]);
   printf("\n");}
}
main()
{clrscr();
print(stu[]);
}

当总是运行时总是显示Expression syntax。有哪位大虾懂的,指点一下,谢咯。
发表于 2005-5-30 16:24 | 显示全部楼层
语法错误。。

stu[N]是一个结构体数组,在printf里却没有数组下标。
主函数里调用print时参数用数组名就行了。

void print(struct student stu[])
{int i;
for(i=0;i<N;i++)
  {printf(FORMAT,stu.num,stu.name,stu.sex,stu.age,
        stu.score[0],stu.score[1],stu.score[2]); //modify here stu. change to stu.
   printf(\"\\n\");}
}
main()
{clrscr();
print(stu); //modify here stu[] change to stu
}

[ Last edited by hjack on 2005-5-30 at 16:31 ]
回复

使用道具 举报

 楼主| 发表于 2005-6-2 23:06 | 显示全部楼层
多谢hjack的支点哦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-6-9 22:47

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

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