You are on page 1of 6

KEY POINTS (STRUCT)

Arrays vs Struct

ARRAY holds several data elements of the same type.

STRUCT holds data items of different data types under a single name for better handling.

• Structures are used to represent a record.


• Structs are generally useful and convenient whenever a lot of data needs to be grouped
together. In database applications, structures are called records.

HOW TO USE STRUCT


(1) Struct Definition

* It is declared outside the main function. It is a global definition.

For Example:
Struct name
#include<stdio.h>

struct STUDENT { Member data:


char NAME[50]; NAME,
char STUD_NUM[20]; STUD_NUM,
int AGE;
AGE,
char ADDRESS[50];
}; ADDRESS

int main (){


.
.
}
(2) Variable Declaration of Struct Type
* after struct definition, you need to declare variable(s) of that struct type for to use the
struct. You can use the struct type anywhere in your code since it was declare as GLOBAL.

struct TYPE var_name;

For Example:

#include<stdio.h>

struct STUDENT {
char NAME[50];
char STUD_NUM[20];
int AGE;
char ADDRESS[50];
};

int main (){ variable student


struct STUDENT student; as struct type
STUDENT
.
.
.

}
(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];
};

int main (){ variable student


struct STUDENT student; as struct type
STUDENT
printf (“Enter student’s name : ”); Access the member
gets (student.NAME);
using dot operator
printf (“Enter student’s ID number: ”);
gets (student.STUD_NUM); var_name.member
printf (“Enter student’s age : ”);
scanf (“%i”, &student.AGE);
getchar();

printf (“Enter student’s address : ”);


gets (student.ADDRESS);

return 0;
}

*NOTE: STUDENT is a STRUCT.


student is a variable of struct type STUDENT

* 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:

(1) struct-to-struct assignment


For Example:

#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;
}

(2) Arrays of Structures


For Example:
#include<stdio.h>

// 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>

// struct definition STUDENT


struct STUDENT {
char NAME[50];
int AGE;
char COURSE[50];

// 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);

printf ("Enter Grades in 5 Subjects\n");


for (int i=0; i<5; i++)
scanf ("%f", &student.GRADE[j]);
}

.
.
.
return 0;
}
(4) Array(s) of Structures with Array(s) inside (as members)

For Example:
#include<stdio.h>

// struct definition STUDENT


struct STUDENT {
char NAME[50];
int AGE;
char COURSE[50];

// 5 grades (array)
float GRADE[5];
};

int main(){
// declare student as struct type STUDENT
// (5 students, as an array)
struct STUDENT student[5];

for (int i=0; i<1; i++){


printf ("Enter Student's Name: ");
gets (student[i].NAME);

printf ("Age : ");


scanf ("%i", &student[i].AGE);
getchar();

printf ("COURSE : ");


gets (student[i].COURSE);

printf ("Enter Grades in 5 Subjects\n");


for (int j=0; j<5; j++){
scanf ("%f", &student[i].GRADE[j]);
}
}
.
.
.

return 0;
}

You can display member data through printf/fprintf statement using DOT operator.
printf (“%i”, student[2].GRADE[0]);

printf (“%s”, student[1].NAME);

* ALL THE REST ARE BASIC. I did not include Pointer to STRUCT since we will not cover
LINKED LISTS.

You might also like