0% found this document useful (0 votes)
5 views

Arrays of Structure

Uploaded by

Aashish Hooda
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Arrays of Structure

Uploaded by

Aashish Hooda
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ARRAYS AS

STRUCTURES

Presented By: Mr. Madhur Thapliyal


Assistant Professor
School of Computing
Department of Computer Applications
GEHU
WHEN WE WERE DOING
STRINGS IN STRUCTURES
 struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};

int main() {
struct myStructure s1;

// Trying to assign a value to the string


s1.myString = "Some text";

// Trying to print the value


printf("My string: %s", s1.myString);

return 0;
}
 prog.c:12:15: error: assignment to expression with
array type
HOWEVER, THERE IS A SOLUTION FOR THIS! YOU
CAN USE THE STRCPY() FUNCTION AND ASSIGN THE
VALUE TO S1.MYSTRING, LIKE THIS:
 struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};

int main() {
struct myStructure s1;

// Assign a value to the string using the strcpy function


strcpy(s1.myString, "Some text");

// Print the value


printf("My string: %s", s1.myString);

return 0;
}
OUTPUT
 My string: Some text
COPY STRUCTURES

 struct myStructure s1 = {13, 'B', "Some text"};


struct myStructure s2;

s2 = s1;
ARRAY OF STRUCTURES VS. ARRAY WITHIN A
STRUCTURE IN C/C++

 A structure is a data type in C/C++ that


allows a group of related variables to be
treated as a single unit instead of separate
entities. A structure may contain elements of
different data types – int, char, float,
double, etc. It may also contain an array as
its member. Such an array is called an array
within a structure. An array within a
structure is a member of the structure and
can be accessed just as we access other
elements of the structure.
EXAMPLE:
 #include <stdio.h>  // Accessing the contents of the
  // array within the structure
 // Declaration of the structure candidate  for (i = 0; i < len; i++) {
 struct candidate {
 int roll_no;
 printf("Subject %d : %.f\n",
 char grade;
 i + 1, a1.marks[i]);
  }
 // Array within the structure  }
 float marks[4]; 
 };  // Driver Code

 int main()
 // Function to displays the content of
 // the structure variables
 {
 void display(struct candidate a1)  // Initialize a structure
 {  struct candidate A
  = { 1, 'A', { 98.5, 77, 89,
 printf("Roll number : %d\n", a1.roll_no); 78.5 } };
 printf("Grade : %c\n", a1.grade); 
 printf("Marks secured:\n");  // Function to display structure
 int i;
 int len = sizeof(a1.marks) /
 display(A);
sizeof(float);  return 0;
  }

OUTPUT:
 Roll number : 1
 Grade : A
 Marks secured:
 Subject 1 : 98.50
 Subject 2 : 77.00
 Subject 3 : 89.00
 Subject 4 : 78.50
ARRAYS OF STRUCTURES
 An array is a collection of data items of the same type.
Each element of the array can be int, char, float, double, or
even a structure. We have seen that a structure allows
elements of different data types to be grouped together
under a single name. This structure can then be thought of
as a new data type in itself. So, an array can comprise
elements of this new data type. An array of structures finds
its applications in grouping the records together and
provides for fast accessing. Below is the demonstration of
an array of structures. The array holds the details of the
students in a class. The details include the roll number,
grade, and marks, which have been grouped under a
structure (record). There exists one record for each
student. This is how a collection of related variables can be
assembled under a single entity for enhancing the clarity of
code and increasing its efficiency.
EXAMPLE:
 #include <stdio.h>

 for (i = 0; i < len; i++) {
 printf("Roll number : %d\n",
 // Declaring a structure class  class_record[i].roll_no);
 struct class {  printf("Grade : %c\n",
 int roll_no;  class_record[i].grade);
 char grade;  printf("Average marks : %.f\n",
 float marks;
 class_record[i].marks);
 printf("\n"); } }
 }; 
  // Driver Code
 // Function to displays the contents  int main()
 // of the array of structures  {
 void display(struct class
 // Initialize of an array of
class_record[3]) structures
 struct class class_record[3]
 {
 = { { 1, 'A', 89.5f },
 int i, len = 3;  { 2, 'C', 67.5f },
  { 3, 'B', 70.5f } };
 // Display the contents of the 
array  // Function Call to display
 // of structures here, each  // the class_record
element  display(class_record);
 // of the array is a structure of  return 0;
class  }
OUTPUT:
 Roll number : 1
 Grade : A
 Average marks : 89.50
 Roll number : 2
 Grade : C
 Average marks : 67.50
 Roll number : 3
 Grade : B
 Average marks : 70.50
THANK YOU

You might also like