C108: structure
Structure is used to group related variable that describes about an entity. Like an array, structure allows more than one items to be stored under same reference name. But the difference is array can only store items of same data type, and structure can store several items of the different data types.
Structure is defined using struct keyword and followed by the structure name. Collection of variables that describe the structure is called members. They are declared like declaring variable, data type and member name.
for example
this structure is called Movie and contain three members. Each member is from different data type.
To access the member of the structure in program, dot operator is used
example program
Output in console:
Sending a structure to function, following program also will produce same output as above in console.
Structure is defined using struct keyword and followed by the structure name. Collection of variables that describe the structure is called members. They are declared like declaring variable, data type and member name.
struct structureName{
// member declaration;
};
// member declaration;
};
for example
struct Movie{
char title[50];
char director[50];
int year;
};
char title[50];
char director[50];
int year;
};
this structure is called Movie and contain three members. Each member is from different data type.
To access the member of the structure in program, dot operator is used
book.title;
example program
#include <stdio.h>
#include<string.h>
struct Movie{
char title[50];
char director[50];
int year;
};
int main(){
struct Movie movie;
strcpy(movie.title, "Pacific Rim");
strcpy(movie.director, Guillermo del Toro");
movie.year = 2013;
printf("Movie title $s\n", movie.title);
printf(" director $s\n", movie.director);
printf(" released on : $s\n", movie.year);
return 0;
}
#include<string.h>
struct Movie{
char title[50];
char director[50];
int year;
};
int main(){
struct Movie movie;
strcpy(movie.title, "Pacific Rim");
strcpy(movie.director, Guillermo del Toro");
movie.year = 2013;
printf("Movie title $s\n", movie.title);
printf(" director $s\n", movie.director);
printf(" released on : $s\n", movie.year);
return 0;
}
Output in console:
Movie title : Pacific Rim
director : Guillermo del Toro
released on : 2013
director : Guillermo del Toro
released on : 2013
Sending a structure to function, following program also will produce same output as above in console.
#include <stdio.h>
#include<string.h>
struct Movie{
char title[50];
char director[50];
int year;
};
void printMovieInformation(struct Movie movie);
int main(){
struct Movie movie1;
strcpy(movie1.title, "Pacific Rim");
strcpy(movie1.director, Guillermo del Toro");
movie1.year = 2013;
struct Movie movie2;
strcpy(movie2.title, "Black Panter");
strcpy(movie2.director, "Ryan Coogler");
movie2.year = 2018;
printMovieInformation(movie1);
printMovieInformation(movie2);
return 0;
}
void printMovieInformation(struct Movie movie){
printf("Movie title $s\n", movie.title);
printf(" director $s\n", movie.director);
printf(" released on : $s\n", movie.year);
}
#include<string.h>
struct Movie{
char title[50];
char director[50];
int year;
};
void printMovieInformation(struct Movie movie);
int main(){
struct Movie movie1;
strcpy(movie1.title, "Pacific Rim");
strcpy(movie1.director, Guillermo del Toro");
movie1.year = 2013;
struct Movie movie2;
strcpy(movie2.title, "Black Panter");
strcpy(movie2.director, "Ryan Coogler");
movie2.year = 2018;
printMovieInformation(movie1);
printMovieInformation(movie2);
return 0;
}
void printMovieInformation(struct Movie movie){
printf("Movie title $s\n", movie.title);
printf(" director $s\n", movie.director);
printf(" released on : $s\n", movie.year);
}
Comments
Post a Comment