structs Box Aggregating associated data into a single variable width length height height Circle int main() { radius Box mybox; Circle c; mybox width = 10; mybox. width = 10; mybox.length = 30; mybox.height = 10; c.radius = 10; } CSE 251 Dr. Charles B. Owen Programming in C 1
23
Embed
structs Box - University of North Floridawkloster/2220/ppts/structs.pdfYou can declare an array of a structure and manipulate each one typedef struct {dbldouble radius; int x; int
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
structs Box
Aggregating associated data into a single variable
I want to describe a box. I need variables for the width, length, and height. height
I can use three variables, but wouldn’t it be better if I had a single variable to describe a better if I had a single variable to describe abox?
That variable can have three parts the widthThat variable can have three parts, the width, length, and height.
CSE 251 Dr. Charles B. OwenProgramming in C2
Structs
A struct (short for structure) in C is a grouping of variables together into a single type
– Similar to structs in Matlab
struct nameOfStruct{
type member;type member;…
}; N t th i l t th d}; Note the semicolon at the end.To declare a variable:
struct nameOfStruct variable_name;
CSE 251 Dr. Charles B. OwenProgramming in C3
Example #include <stdio.h>
struct Box
Boxidth
struct Box {
int width;int length;
Data structure
widthlengthheight
int length;int height;
};
l
definition
Circle
struct Circle {
double radius;Circle
radius};
int main() {
You can declare
i bl{struct Box b; struct Circle c;
}
variables
CSE 251 Dr. Charles B. OwenProgramming in C4
}
A
Example
int main() {struct Box b; You can
assign values
#include <stdio.h>
struct Box {
b.width = 10;b.length = 30;b.height = 10;
gto each member
int width;int length;int height;
};
Box
b.height 10;
}
};
widthlengthheight
We use a period “.” to get to the elements of a struct. g
If x is a struct, x.width is an element in a struct
CSE 251 Dr. Charles B. OwenProgramming in C5
in a struct.
Another Examplet t b kR dSt t
You can use mixed data types
struct bankRecordStruct{
char name[50]; within the struct (int, float, char [])
char name[50];float balance;
};};
struct bankRecordStruct billsAcc;
CSE 251 Dr. Charles B. OwenProgramming in C6
Accessing valuest t b kR dSt tstruct bankRecordStruct{
char name[50];Access values in a struct using a period:
float balance;};
“.”
struct bankRecordStruct billsAcc;
i tf(“M b l i %f\ ” bill A b l )printf(“My balance is: %f\n”, billsAcc.balance);
float bal = billsAcc.balance;
CSE 251 Dr. Charles B. OwenProgramming in C7 B
Assign Values using Scanf()t t k dstruct BankRecord
{char name[50];float balance;;
};
int main() (){
struct BankRecord newAcc; /* create new bank record */
i tf(“E t t “)printf(“Enter account name: “);scanf(“%50s”, newAcc.name);printf(“Enter account balance: “);scanf(“%d”, &newAcc.balance);scanf( %d , &newAcc.balance);
}
CSE 251 Dr. Charles B. OwenProgramming in C8
Copy via =
You can set two struct type variables equal to each other and each element will be copied
struct Box { int width, length, height; };
int main() {
struct Box b, c;b.width = 5; b.length=1; b.height = 2;c = b; // copies all elements of b to cc = b; // copies all elements of b to cprintf(“%d %d %d\n”, c.width, c.length, c.height);
}
CSE 251 Dr. Charles B. OwenProgramming in C9
Passing Struct to a function
• 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!
int myFunction(struct Person p){…}
CSE 251 Dr. Charles B. OwenProgramming in C10
Using Structs in Functions
Write a program that – Prompts the user to enter the dimensions of a 3D box and a circle
– Prints the volume of the box and area of the circle
Sample run:
CSE 251 Dr. Charles B. OwenProgramming in C11
#include <stdio.h>#include <math.h>
struct Box { int width height length; };struct Box { int width, height , length; };