Docsity

Understanding Structs in C: Box and Circle Examples, Summaries of C programming

Partial preview of the text

Download Understanding Structs in C: Box and Circle Examples and more Summaries C programming in PDF only on Docsity!

structs^

Box

Aggregating^ associated

data into^ a^ single^ variable

widthlengthheightheight 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.^ OwenProgramming^ in^ C

The^ idea^

Boxwidthlengthheight

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

Example^

#include^ <stdio.h>struct Box

Boxidth

struct^ Box { int width;int length;

Data structure widthlengthheight

int^ length;^ definition int height;}; l

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^ C } 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;

g to 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^ C in^ a^ struct.

Accessing^ values^ t^ t^ b^ kR^ dSt

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

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^ C

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^ C

Using^ Structs^ in^ FunctionsWrite^ 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^ C

Note:^ ==^ Comparison

doesn’t^ work

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 )

Create^ your^ own^ equality

test

#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

typedef for^ ArraysThere^ is^ a^ special^ syntax

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

Using^ Structs^ with

Typedef

typedef struct [nameOfStruct] {^ type member;type member;

optional

yp … } TypeName ;

optional

}^ yp^ ;^ To^ declare^ a^ variable:

TypeName^ variable_name;^ CSE^251

Dr.^ Charles^ B.^ OwenProgramming^ in^ C

Arrays^ of^ structsYou^ can^ declare^ an^

array^ of^ a^ structure^

and^ manipulate^ each

one typedef struct{ d^ bl^ didouble^ radius;int x;int y;h^ [10]char^ name[10];}^ Circle;Circle^ circles[5];

CSE^251 Dr.^ Charles^ B.^ OwenProgramming^ in^ C

Size^ of^ a^ Struct:^ sizeof typedef struct{ double^ radius;

/*^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