QUESTION 01
What is a structure?
2 marksAnswer
A structure groups named members, possibly of different types, into one user-defined aggregate type. Each object contains its own member objects.
QUESTION 01
A structure groups named members, possibly of different types, into one user-defined aggregate type. Each object contains its own member objects.
QUESTION 02
A structure represents related data as one record, such as a student’s roll number and marks.
struct Student {
int roll;
double marks;
};
struct Student s = {101, 87.5};The tag Student names the structure type. The definition lists an int member roll and a double member marks. s is an object of that type. Use s.roll and s.marks to select its members. A member assignment such as s.marks = 90.0 changes only that member of s.
The braces supply initial values in member order. A designated initialiser, such as {.marks = 87.5, .roll = 101}, can name members explicitly. Storage may include padding for alignment, so sizeof(struct Student) need not equal the sum of the written member sizes. Do not assume a fixed byte layout across computers.
Accessing a fixed member takes O(1) work. Processing n records generally requires O(n) traversal work. Structure definition alone does not execute input or output; the program must read and display members explicitly.
/* CodeBhavya | Data Structures AR25 | Learning example */
#include <stdio.h>
struct Student { int roll; double marks; };
int main(void)
{
struct Student s = {101, 87.5};
printf("Roll = %d, marks = %.1f\n", s.roll, s.marks);
return 0;
}The example uses values in the code.
Roll = 101, marks = 87.5