The structure mechanism allows us to aggregate variables of different types
struct definitions are usually placed outside of functions so that they are in scope throughout the file, as in the following example:
The “.” in a.num is the “structure member operator”, which connects the structure name and the member name.
A “member” is a variable in a structure.
Assignment (=) works just as you would expect, as if there were a separate assignment for each structure member.
Structures Simple Example
struct card_struct {
int number;
char suit;
}; /* note the semicolon after the definition! */
void some_function() {
struct card_struct a, b;
a.number = 3;
a.suit = ’D’;
b = a;
}
Structure member as Parameter
void sum(double p1_x,double p1_y,double p2_x,double p2_y ) {
struct point psum;
psum.x = p1_x + p2_x;
psum.y = p1_y + p2_y;
printf(“%lf_%lf\n”, psum.x, psum.y); 10_5
}
void main()
{
{
struct point a, b, c;
a.x = 3.5; a.y = 4.5; b.x = 6.5; b.y = 0.5;
printf(“%lf_%lf_%lf_%lf\n”, a.x, a.y, b.x, b.y);
sum( a.x, a.y, b.x, b.y); 3.5_4.5_6.5_0.5
}
Structure as Parameter
Structures work seamlessly with functions.
A structure is a type, so it can be the type of a function parameter (as here), or a return type:
point sum( struct point p1, struct point p2 ) {
struct point psum;
psum.x = p1.x + p2.x;
psum.y = p1.y + p2.y;
return psum;
}
void main() { 3.5_4.5_6.5_0.5_10_5
struct point a, b, c;
a.x = 3.5; a.y = 4.5; b.x = 6.5; b.y = 0.5;
c = sum( a, b);
printf(“%lf_%lf_%lf_%lf_%lf_%lf\n”, a.x, a.y, b.x, b.y, c.x, c.y);
}
Post by j.siam,
Ref-: Md Munirul Haque