i want to show you about Struct in C.
Struct:
- Another user defined data type available in C that allows to combine data items of different kinds.
Structure is the collection of variables of different types under a single name for better handling. For example: You want to store the information about peoples name, age and their citizenship. You can create these information separately but, better approach will be collection of these information under single name because all these information are related to person.
Syntax of structure:
{
data_type member1;
data_type member2;
.
.
data_type member;
};
We can create the structure for a person as mentioned above as:
struct person
{
char name[5];
int cit_no, age ;
};
This declaration above creates the derived data type struct person.
Relation between Struct and Array
Sometimes, we will do a task with data that has some item, but it differs from array. For example, identity card has data like name, adress, sex, and age. That is not suitable if we use array, because array only can has some element with the same type. That example can be used in a struct.
Example:
You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
Example:
That's the Structs in C. Hope this will help you. Thanks! :D
nb: Some source that really helps me very much (recommended):
http://www.programiz.com/c-programming/c-structures
http://stackoverflow.com/questions/10370047/passing-struct-to-functio
http://www.tutorialspoint.com/cprogramming/c_structures.htm
Tidak ada komentar:
Posting Komentar