0

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

1
  • 3
    stuwise(s a); is wrong. Replace with stuwise(a); or stuwise(&a[0]); Commented Jul 15, 2014 at 15:57

1 Answer 1

1

stuwise(s a); 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]);
Sign up to request clarification or add additional context in comments.

5 Comments

Okay changed that but I am getting an error "subscripted value is neither array nor pointer nor vector" printf("%d ",(a[j].marks[m])); But I have already declared a as an array and the function is also called with base address of array.What's wrong ?
@thetechfreak Pointers to structs don't use . to get elements of the struct, but use ->.
Yep I had tried with that also but got the same error. I guess the 2 functions just aren't able to recognize a as an array , instead treating it as a variable. Strange
I tried both my solutions and your code worked fine. Did you modify anything else in the interim?
Ah figured it out. Yep my bad. Thanks :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.