Aggregating associated data into single variable
void main(){ Box box; Circle c; box.with = 10; box.length = 30; box.heigth = 10; c.radius = 10; }
Box |
---|
Width length height |
Circle |
---|
radius |
I want to describe a box. I need variables for the width, length, and height.
I can use three variable, but would't it be better if i had a single variale to describe a box?
That variable can have three parts, the width, length, and height.
Box |
---|
Width length height |
A struct (short for structure) in C is grouping of variable together into a single type
Syntax
struct nameOfStruct{ type member; type memer; .... };
Note the semicolon at the end.
To declare a variable: struct nameOfStruct variable_name;
Box |
---|
Width length height |
Circle |
---|
radius |
//Data structure defination struct Box{ int width; int length; int height; }; struct Circle{ double radius; }; void main(){ //Declare variables struct Box b; struct Circle c; }
struct Box{ int width; int height; int length; };
Box |
---|
Width length height |
void main(){ struct Box b; //You can assign values to each member b.width = 10; b.length = 30; b.height = 10; }
We use a period "." to get the elements of a struct.
If x is a struct, x.width is an element in a struct.
struct BankRecord{ char name[50]; float balance; }; struct BankRecord billsAcc;
You can use mixed data types within the struct (int, float, char[])
Access values in a struct using period: "."
struct BankRecord{ char name[50]; float balance; }; struct BankRecord billsAcc; printf("My balance is: %f", billsAcc.balance); float bal = billsAcc.balance;
struct BankRecord{ char name[50]; float balance; }; void main(){ struct BankRecord acc; //create new bank record printf("Enter account name: "); scanf("%50s", acc.name); printf("Enter account balance: "); scanf("%f", &acc.balance); }
You can set two struct type variables equal to each other and each element will be copied
struct Box{ int width, length, height; }; void main(){ struct Box b, c; b.width = 5; b.length = 1; b.height = 2; c = b; //copies all elements of b to c printf("%d %d %d\n", c.width, c.length, c.height); }
You can pass a struct to a function. All the elements are copied
If an element is a pointer, the pointer is copied but not what it points to!
void f(struct Box b){ //do something }
Write a program that
Prompts the user to enter the dimensions of 3D box and a circle.
Prints the volume of the box and area of the circle.
Sample run:
Enter the box dimensions (width, length, height): 1 2 3
Enter the radius of ther circle: 0.8
Box volume = 6
Circle are = 2.01
struct Box{ int width, height, length; }; int getVolume(struct Box b){ return b.width + b.height + b.length; } void main(){ struct Box b; printf("Enter the box dimensions (width, length, height): "); scanf("%d %d %d", &b.width, &b.length, &b.height); printf("Box volume = %d\n", getVolume(b)); }
struct Box{ int width, height, length; }; void main(){ struct Box b, c; b.width = 5, b.length = 1, b.height = 2; c = b; if(c == b){ //Error when you compile! printf("c and b are identical\n"); }else{ prinf("c and b are difference\n"); } } //Error message: invalid operands to binary == (Have 'Box' and 'Box')
#include <stdbool&tg; struct Box{ int width, length, height; }; bool isEqual(struct Box b, struct Box c){ return b.width == c.width && b.length == c.length && b.height == c.height; }
typedef is a way in C to give a name to a custom type.
Syntax: typedef type newName;
typedef int dollars; typedef unsigned char byte; //I can declare variable like dolors d; byte b, c;
It's as if the type already existed
There is a special syntax for arrays:
typedef char names[40]; typedef double vector[4]; typedef double matrix4x4[4][4]; //Now, instead of: double mat[4][4]; //I can do: matrix4x4 mat;
syntax:
typedef struct [nameOfStruct]{//optional type member; type member; ... }TypeName;
To declare a variable: TypeName variable_name
Box |
---|
Width length height |
Circle |
---|
radius |
typedef struct{ int width, height, length; }Box; typedef struct{ double radius; }Circle; void main(){ Box b; //Instead of struct Box Circle c; //Instead of struct Circle b.with = 10; b.length = 30; b.height = 10; c.radius 10; }
You can declare an array of a structure and minupalate each one
typedef struct{ double radius; }; Circle circles[5];
typedef struct{ char name[10]; //8 bytes int age; // 4 bytes char gender;//1 bytes }Student; printf("Size of circle struct is %ld\n", sizeof(Student));
typedef struct{ int width, length, height; }Box; Box b; //A variable of type Box Box *c; //A pointer to a Box double w; b.width = 5; b.height = 7; b.length = 3; c = &b; //Same as before w = c->width;
To access the members of a struct, we use:
"."" for variable of the struct's type
"->" for a pointer to a struct
typedef struct{ double width, height; }Box; typedef struct{ double radius; }Circle;
Box b; Circle c; Box* pb; Circle* pc; pb = &b; b.width = 3; pb->width = 7; pc = &c; (*pc).radius = 9;