Showing posts with label Member Variables. Show all posts
Showing posts with label Member Variables. Show all posts

Monday, March 28, 2016

Array of Structures

When we have to declare similar kind of user defined data types in C, we can use array of structures. As you know have already seen that, arrays are used to store similar kinds of data. In all aspects, the array of structures is same as the other arrays i.e. arrays of integers, float etc. Only you have to use dot(.) operator or reference operator(->) to access structure members.
For example, we can define an array to store 100 student details like below.
student array_of_student[100];
Now, to access the member variables, we have to use array index number and the dot operator. For example, to set and print the value of 2'nd student name, roll number would be like this:
strcpy(array_of_student[1].name,”Denis Ritchie”);
array_of_student[1].roll_number = 20;
printf(“Student’s Name : %s\n array_of_student[1].name);
printf(“Student’s Roll Number : %d\n”, array_of_student[1].roll_number);

Thursday, March 24, 2016

Member Variables

All the subfields i.e the primitive data type variables in structures are called member variables. We can access these member variables using dot(.) operator. We can set and retrieve the member variable of student structure as shown below.
student student_1 = {0};
student_1.roll_number = 20;
strcpy(student_1.name,”Denis Ritchie”);
student_1.class = 12;
student_1.section = ‘A’;
student_1.marks = 96.9;
strcpy(student_1.school_name,”a4academics.com”);