简体   繁体   中英

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' "

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM