Q:

Write a C Program to understand arrays within structures

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Arrays Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to understand how array of struc... >>
<< Write a C Program to enter Student Details using a...