Write a C Program to understand arrays within structures

By | 20.03.2017

C Program to understand arrays within structures


Write a C Program to understand arrays within structures. Here’s a Simple Program to understand arrays within structures in C Programming Language.


Arrays within Structures


Sometimes, arrays may be the member within structure, this is known as arrays within structure. Accessing arrays within structure is similar to accessing other members.

When you find yourself to store a string value, then you have to go for array within structure. because your name comes under character data type alone, thus array is capable of storing data of same data type.

As we know, structure is collection of different data type. Like normal data type, It can also store an array as well.


Syntax for array within structure


// Syntax for array within structure           


struct struct-name
              {
                     datatype var1;                    // normal variable
                     datatype array [size];          // array variable
                     - - - - - - - - - -
                     - - - - - - - - - -
                     datatype varN;
              };

              
struct struct-name obj;

Below is the source code for C Program to understand arrays within structures which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* Program to understand arrays within structures*/
#include<stdio.h>
struct student{
                char name[20];
                int rollno;
                int submarks[4];
                };
int main( )
{
        int i, j;
        struct student stuarr[3];
        for(i=0; i<3; i++)
        {
                printf("Enter data for student %d\n", i+1);
                printf("Enter name : ");
                scanf("%s", stuarr[i].name );
                printf("Enter roll number : ");
                scanf("%d", &stuarr[i].rollno);
                for(j=0; j<4; j++)
                {
                        printf("Enter marks for subject %d : ", j+1);
                        scanf("%d", &stuarr[i].submarks[j] );
                }
        }
        for(i=0; i<3; i++)
        {
                printf("Data of student %d\n", i+1);
                printf("Name : %s, Roll number : %d\nMarks : ", stuarr[i].name, stuarr[i].rollno);
                for(j=0; j<4; j++)
                        printf("%d   ", stuarr[i].submarks[j] );
                printf("\n");
        }

    return 0;
}

OUTPUT  : :


Enter data for student 1

Enter name : John
Enter roll number : 1
Enter marks for subject 1 : 56
Enter marks for subject 2 : 6
Enter marks for subject 3 : 78
Enter marks for subject 4 : 78

Enter data for student 2

Enter name : Max
Enter roll number : 2
Enter marks for subject 1 : 56
Enter marks for subject 2 : 45
Enter marks for subject 3 : 78
Enter marks for subject 4 : 98

Enter data for student 3

Enter name : AJ
Enter roll number : 3
Enter marks for subject 1 : 45
Enter marks for subject 2 : 67
Enter marks for subject 3 : 89
Enter marks for subject 4 : 45

Data of student 1
Name : John, Roll number : 1
Marks : 56   6   78   78

Data of student 2
Name : Max, Roll number : 2
Marks : 56   45   78   98

Data of student 3
Name : AJ, Roll number : 3
Marks : 45   67   89   45

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post.

4.8 4 votes
Article Rating
Category: Arrays Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Srikanth Reddy N

The best website tht I have ever been