C语言 结构体数组 计算个数

自己定义 然后在main函数里面第一变量struct student stu[100]; 然后自己随机输入数据 要怎么计算出自己输入了多少个结构体数组
struct student
{
int num;
char name[20];
char sex[3];
}
定义变量 打错了

第一种方法,设置一个结构体变量的成员为某个具体的常量,进行遍历寻找得出变量的数量
第二种方法,在输入时计算
第三种,建立一个有指针域的动态链表

用第三种方法实现的一个例子,可用来学籍管理系统
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct student
{
int num;
char name[20];
char sex[3];
struct student *next;
}LIST;

LIST *CreateList()
{
int i,n;
LIST *head=NULL,*pre,*cur;
printf("请输入学生的总个数:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("正在输入第%d个学生数据!\n",i);
pre=(LIST *)malloc(sizeof(LIST));
pre->next=NULL;
printf("请输入该学生的姓名:");
scanf("%s",&pre->name);
printf("请输入该学生的学号:");
scanf("%d",&pre->num);
printf("请输入该学生的性别:");
scanf("%s",&pre->sex);

if(NULL==head)
head=pre;
else
cur->next=pre;
cur=pre;
}
return head;
}

int GetNum(LIST *head)
{
int i=1;
while(head->next!=NULL)
{
i++;
head=head->next;
}
return i;
}

void Disp(LIST *head)
{
int i=1;
printf("正在输出学生信息!\n");
while(head->next!=NULL)
{
printf("正在输入第%d个学生的数据!\n",i++);
printf("学生姓名:\t%s\n学生学号:\t%d\n学生性别:\t%s\n",head->name,head->num,head->sex);
head=head->next;
}
printf("正在输入第%d个学生的数据!\n",i++);
printf("学生姓名:\t%s\n学生学号:\t%d\n学生性别:\t%s\n",head->name,head->num,head->sex);
printf("学生信息输出结束!\n");
}

void DestroyList(LIST *head)
{
LIST *pre=head;
while(head->next!=NULL)
{
pre=pre->next;
free(head);
head=pre;
}
}

int main(void)
{
LIST *head;
head=CreateList();
printf("一共有%d个学生数据!\n",GetNum(head));
Disp(head);
DestroyList(head);
getch();
return 0;
}

满意采纳,不满意追问
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-10
法一: 可以自己定义一个计数器 int count=0; 在循环输入过程中,使用它来进行计数。
法二:在开始过程中将 stu[100]的char name[20]全部初始化为\0,输入完毕后,在循环依次判断是否为\0,统计数字。
建议使用法一。
不过楼上说得对,这题确实没有太大的意义,如果是动态数组的话,还有点意思。这静态的,没意思。本回答被网友采纳
第2个回答  2013-06-10
给数组初始化,判断值是否被改变-----------------这题没意义的
第3个回答  2013-06-10
那你再定义一个计数变量i用于计数,
相似回答