















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Aggregating^ associated
data into^ a^ single^ variable
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.^ OwenProgramming^ in^ C
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 a box? 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^ C
#include^ <stdio.h>struct Box
struct^ Box { int width;int length;
Data structure widthlengthheight
int^ length;^ definition int height;}; l
struct Circle { double^ radius;
}; int main() {
You^ can declare^ i bl {^ struct Box^ b;^ struct Circle^ c; }
variables CSE 251 Dr. Charles B. OwenProgramming in^ C } A
int main()^ { struct Box^ b;^
You^ can assign^ values #include^ <stdio.h>struct Box {
b.width =^ 10;b.length =^ 30;b.height = 10;
g to each member
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^ C in^ a^ struct.
t struct 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^ C 7 B
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^ C
CSE^251 Dr.^ Charles^ B.^ OwenProgramming^ in^ C
to^ enter^ the^ dimensions
of^ a^ 3D^ box^ and a^ circle – Prints^ the^ volume^ of^ the
box^ and^ area^ of^ the^ circle
CSE^251 Dr.^ Charles^ B.^ OwenProgramming^ in^ C
struct Box^ {^ int width,
length,^ height;^ }; int^ main()int^ main() { struct Box^ b,^ c; b width^ 5; b length 1; b height
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”); elseprintf(“c and b are different\n”);printf( c^ and^ b
are^ different\n ); }^ t^ Error message: invalid operands to binary == (have
'Box' and 'Box') CSE 251 Dr.^ Charles^ B.^ OwenProgramming^ in^ C Error^ message:^ invalid 13 operands^ to^ binary^ ==^ (have
Box^ and^ Box )
#i^ l d^ <^ tdi^ h>#include^ <stdio.h>#include^ <math.h>struct Box^ {^ int width,
height^ ,^ length;^ };{ , g^ ,^ g^ ; }; int IsEqual(struct Box
b,^ struct Box^ c) { if (b width==c width &&if^ (b.width==c.width &&b.length==c.length &&b.height==c.height)return^ 1;^
b struct Boxelsereturn 0;} b,^ c;b.width = 5;^ b.length=1;^ b.height =
2; c^ =^ b; if^ (IsEqual(b,c))printf("c^ and^ b^ are
identical\n"); elsei tf("^ d b^
diff^ t\ ") CSE 251 Dr. Charles^ B.^ OwenProgramming^ in^ C printf("c^ and^ b^ are^ diff
erent\n"); D
for^ arrays:^ Now,^ instead
of: typedef char^ Names[40];typedef double^ Vector[4];typedef double Mat4x4[4][4];
double^ mat[4][4]; typedef double^ Mat4x4[4][4];
I^ can^ do:^ Mat4x4 mat;Mat4x4^ mat;^ CSE^251 Dr.^ Charles^ B.
OwenProgramming^ in^ C
optional
optional
TypeName^ variable_name;^ CSE^251
Dr.^ Charles^ B.^ OwenProgramming^ in^ C
CSE^251 Dr.^ Charles^ B.^ OwenProgramming^ in^ C
/*^8 bytes
int x;^
/*^4 bytes^ */ int y;^
/ 4 bytes / int y;^
/*^4 bytes^ */ char^ name[10];^
/*^10 bytes^ */ } Circle;}^ Circle; printf(“Size^ of^
Circle^ struct is
%d\n”, sizeof(Circle));sizeof(Circle));
CSE^251 Dr.^ Charles^ B.^ OwenProgramming^ in^ C