我想在我的函数中传递一个结构作为参数但在传递它时遇到一些问题。
没有功能的代码是:
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[100];
char roll[100];
int marks[5];
}a[3];
typedef struct student s;
void stuwise(s *a);
void subwise(s *a);
int i;
int j;
int m;
int main(int argc, char const *argv[])
{
for(i=0;i<3;i++)
{
printf("Enter Student %d Name \n",i+1);
fgets(a[i].name,100,stdin);
printf("Enter Student %d Roll Number \n",i+1);
fgets(a[i].roll,10,stdin);
for(m=0;m<5;m++)
{
printf("Enter Student %d Marks %d\n",i+1,m+1);
scanf("%d",&(a[i].marks[m]));
getchar();
}
}
printf("Student wise list :\n");
for(j=0;j<3;j++)
{
for(m=0;m<5;m++)
{
printf("Student %d Marks %d ",j+1,m+1);
printf("%d ",(a[j].marks[m]));
printf("\n");
}
printf("\n");
}
printf("Subject wise list :\n");
for(m=0;m<5;m++)
{
for(j=0;j<3;j++)
{
printf("Student %d Marks %d ",j+1,m+1);
printf("%d ",(a[j].marks[m]));
printf("\n");
}
printf("\n");
}
return 0;
}
我尝试使用功能:
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[100];
char roll[100];
int marks[5];
}a[3];
typedef struct student s;
void stuwise(s *a);
void subwise(s *a);
int i;
int j;
int m;
int main(int argc, char const *argv[])
{
for(i=0;i<3;i++)
{
printf("Enter Student %d Name \n",i+1);
fgets(a[i].name,100,stdin);
printf("Enter Student %d Roll Number \n",i+1);
fgets(a[i].roll,10,stdin);
for(m=0;m<5;m++)
{
printf("Enter Student %d Marks %d\n",i+1,m+1);
scanf("%d",&(a[i].marks[m]));
getchar();
}
}
stuwise(s a);
return 0;
}
void stuwise(s *a)
{
printf("Subject wise list :\n");
for(m=0;m<5;m++)
{
for(j=0;j<3;j++)
{
printf("Student %d Marks %d ",j+1,m+1);
printf("%d ",(a[j].marks[m]));
printf("\n");
}
printf("\n");
}
}
void subwise(s *a)
{
printf("Student wise list :\n");
for(j=0;j<3;j++)
{
for(m=0;m<5;m++)
{
printf("Student %d Marks %d ",j+1,m+1);
printf("%d ",(a[j].marks[m]));
printf("\n");
}
printf("\n");
}
}
由于我收到错误而无法正常工作&#34;在's'&#34;
之前的预期表达答案 0 :(得分:1)
stuwise(s a);
是调用函数的错误方法。您不必指定变量的类型,因为您已在函数定义中说明了这一点。
替换为:
stuwise(a);
或
stuwise(&a[0]);