Professional Documents
Culture Documents
Key Points (Struct) : #Include
Key Points (Struct) : #Include
Arrays vs Struct
STRUCT holds data items of different data types under a single name for better handling.
For Example:
Struct name
#include<stdio.h>
For Example:
#include<stdio.h>
struct STUDENT {
char NAME[50];
char STUD_NUM[20];
int AGE;
char ADDRESS[50];
};
}
(3) Access the MEMBER DATA
* The members of a struct type variable are accessed with the dot (.) operator:
* After declaring a variable of that struct type, you can now access its member(s) using DOT
operator.
variable_name.member
For Example:
#include<stdio.h>
struct STUDENT {
char NAME[50];
char STUD_NUM[20];
int AGE;
char ADDRESS[50];
};
return 0;
}
* NOTE: Access the members of the struct, using the variable name (of that struct type) (e.g. student),
NOT the name of the struct (e.g. STUDENT.
For example:
student.ADDRESS
NOT: STUDENT.ADDRESS
OTHER OPERATIONS:
#include<stdio.h>
// struct definition
struct Date {
int day;
int month;
int year;
};
int main(){
struct Date date1, date2;
date1.day = 31;
date1.month = 12;
date1.year = 2014;
date2 = date1;
.
.
.
return 0;
}
// struct definition
struct Date {
int day;
int month;
int year;
};
date array of size 100;
int main(){ Of struct type Date
struct Date date[100];
date[5].day = 31;
date[5].month = 12;
date[5].year = 2020;
date element at
index 5;
.
Each element has .
member data: day, .
month, year return 0;
}
(3) Arrays inside Structures
* We can use arrays inside structures.
For Example:
#include<stdio.h>
// 5 grades (array)
float GRADE[5];
};
int main() {
struct STUDENT student;
printf ("Enter Student's Name: ");
gets (student.NAME);
printf ("Age : ");
scanf ("%i", &student.AGE);
getchar();
printf ("COURSE : ");
gets (student.COURSE);
.
.
.
return 0;
}
(4) Array(s) of Structures with Array(s) inside (as members)
For Example:
#include<stdio.h>
// 5 grades (array)
float GRADE[5];
};
int main(){
// declare student as struct type STUDENT
// (5 students, as an array)
struct STUDENT student[5];
return 0;
}
You can display member data through printf/fprintf statement using DOT operator.
printf (“%i”, student[2].GRADE[0]);
* ALL THE REST ARE BASIC. I did not include Pointer to STRUCT since we will not cover
LINKED LISTS.