简体   繁体   中英

2d array of Structs in C

//Getting student details. There are 2 classes and each class has 5 students.
#include<stdio.h>
#include<string.h>

struct student
{
    int rollNo;
    char name[20];
    int subjects[4][3];
};

int main()
{
    struct student s1[3][5]={
        {{1,"Vvvv",{{90,95,111},{85,94,111},{99,98,100}}},
         {2,"Mmmm",{{90,95,97},{85,94,100},{99,98,100}}},
         {3,"Innn",{{90,95,97},{85,94,100},{99,98,100}}},
         {4,"Ssnn",{{90,95,97},{85,94,100},{99,98,100}}},
         {5,"Kkkk",{{90,95,98},{85,94,97},{99,98,100}}}
        },
        {{11,"Arrr",{{90,95,99},{85,94,100},{99,98,100}}},
         {12,"Maaa",{{90,95,99},{85,94,99},{99,98,100}}},
         {13,"Mooo",{{90,95,97},{85,94,100},{99,98,100}}},
         {14,"Mnnn",{{90,95,97},{85,94,100},{99,98,100}}},
         {15,"Arii",{{90,95,99},{85,94,99},{99,98,100}}}
        }
    };
    
    printf("%d %d\n",sizeof(s1),sizeof(struct student));
    printf("---Student Details---\n");
   
    
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<5;j++)
        {
            printf("Roll No. : %d\n",s1[i][j].rollNo);
            printf("Name : %s\n",s1[i][j].name);
    
            for(int i=0;i<3;++i)
            {
                printf("\nSubject%d :\n",i+1);
                for(int j=0;j<3;++j)
                {
                    printf("Exam%d = %d ",j+1,s1[i][j].subjects[i][j]);
                }
                printf("\n");
            }
            printf("\n");
        }
        
        printf("\n\n");
    }
    
    return 0;
}

->Output:

1080 72
Student Details---
Roll No. : 1
Name : Vvvv

Subject1 :
Exam1 = 90 Exam2 = 95 Exam3 = 97

Subject2 :
Exam1 = 85 Exam2 = 94 Exam3 = 100

Subject3 :
Exam1 = 0 Exam2 = 0 Exam3 = 0

Roll No. : 2
Name : Mmmm

Subject1 :
Exam1 = 90 Exam2 = 95 Exam3 = 97

Subject2 :
Exam1 = 85 Exam2 = 94 Exam3 = 100

Subject3 :
Exam1 = 0 Exam2 = 0 Exam3 = 0

Roll No. : 3
Name : Innn

Subject1 :
Exam1 = 90 Exam2 = 95 Exam3 = 97

Subject2 :
Exam1 = 85 Exam2 = 94 Exam3 = 100

Subject3 :
Exam1 = 0 Exam2 = 0 Exam3 = 0

Roll No. : 4
Name : Ssnn

Subject1 :
Exam1 = 90 Exam2 = 95 Exam3 = 97

My questions are why am I getting 0 for subjects[2][0] , subjects[2][1] and subjects[2][2] ?

But when I declare dimensions if array subjects within structure as 3 x 2(extra row to avoid error in memory allocation) and struc student variable s1 as 3 x 3 x 2 (same here extra row and table). So, when I declare like this I am getting correct output.

Please anyone could tell me the reason why am I not getting correct output and what should I do to get correct output?

Thank you

Its happening because you assigned same variables i,j in the inner for loops. Lets say, in the outer loop, at some moment, i = 2, j = 3 , but after it goes to inner loop, it again becomes 0 because (int i=0, int j=0) . And not to mention the scenarios with condition checking.

 for(int i=0;i<2;i++)
    {
    for(int j=0;j<5;j++)
    {
        printf("Roll No. : %d\n",s1[i][j].rollNo);
        printf("Name : %s\n",s1[i][j].name);
        // HERE IS THE ERROR
        for(int i=0;i<3;++i)
        {
            printf("\nSubject%d :\n",i+1);
        // ALSO HERE
            for(int j=0;j<3;++j)
            {
                printf("Exam%d = %d ",j+1,s1[i][j].subjects[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    
    printf("\n\n");
}  

Here is a correct version of your code.

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