[英]passing struct as argument in function
I want to pass a structure as an argument in my function but having some problems in passing it. 我想在我的函数中将结构作为参数传递,但是在传递结构时遇到一些问题。
The code without functions is : 没有功能的代码是:
#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;
}
My attempt at using functions : 我尝试使用功能:
#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");
}
}
It isn't working properly as I am getting an error " expected expression before 's' " 它无法正常工作,因为我收到一个错误“ s之前的预期表达式”
stuwise(sa);
is the wrong way to call the function. 是调用函数的错误方法。 You don't have to specify what type the variable is, since you already said so in the function definition.
您不必指定变量的类型,因为您已经在函数定义中说明了这一点。
Replace with: 用。。。来代替:
stuwise(a);
or 要么
stuwise(&a[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.