+ All Categories
Home > Documents > structs Box - University of North Floridawkloster/2220/ppts/structs.pdfYou can declare an array of a...

structs Box - University of North Floridawkloster/2220/ppts/structs.pdfYou can declare an array of a...

Date post: 12-Jul-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
23
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
Transcript
Page 1: 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

structs Box

Aggregating associated data into a single variable

widthlengthheightheight

Circleint main() {

radiusBox 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 C1

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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.

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

#include <stdio.h>#include <math.h>

struct Box { int width height length; };struct Box { int width, height , length; };

int GetVolume(struct Box b) {

return b.width * b.height * b.length;}

int main()int 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));printf( Box volume   %d\n , GetVolume(b));}

CSE 251 Dr. Charles B. OwenProgramming in C12 C

Page 13: 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

Note:  == Comparison doesn’t workstruct Box { int width, length, height; }; 

int main()int main() {

struct Box b, c;b width 5; b length 1; b height 2;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

printf(“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 C13

Error message: invalid operands to binary == (have  Box  and  Box )

Page 14: 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

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;

belsereturn 0;

}

struct Box 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 C14

printf("c and b are different\n");

D

Page 15: 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

typedef

typedef is a way in C to give a name to a custom type. 

typedef type newname;

typedef int dollars;typedef int dollars;typedef unsigned char Byte;

I can declare variables like:

dollars d;Byte b c;Byte b, c;

It’s as if the type already existed.

CSE 251 Dr. Charles B. OwenProgramming in C15

Page 16: 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

typedef for Arrays

There 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 C16

Page 17: 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

Using Structs with Typedef

typedef struct [nameOfStruct] {

type member;type member; optionalyp…

} TypeName;

optional

} yp ;

To declare a variable:  TypeName variable_name; 

CSE 251 Dr. Charles B. OwenProgramming in C17

Page 18: 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

Example#include <stdio.h>

typedef struct{Box

widthl th

{int width;int length;i t h i htlength

height

Ci l

int height;} Box;

typedef struct {  double radius; } Circle;Circle

radius

yp { }

int main() {

Box b; /* instead of struct Box */Box b;  /* instead of struct Box */Circle c; /* instead of struct Circle */b.width = 10;b l th 30b.length = 30;b.height = 10;c.radius = 10;

}

CSE 251 Dr. Charles B. OwenProgramming in C18

}

E

Page 19: 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

Arrays of structs

You 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 C19

Page 20: 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

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 C20

Page 21: 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

Size of a Struct8 + 4 + 4 + 10 = 26

– But sizeof() reports 28 bytes!!!

Most machines require alignment on 4‐byte boundary ( d)(a word)

– last word is not filled by the char (2 bytes used, 2 left over)

D D D D D D D D I I I I I I I I C C C C C C C C C C X X8 byte, 2 word double 4 byte, 

1 word4 byte, 1 word

10 byte char array, 2 bytesof the last word unused

integer integerof the last word unused

CSE 251 Dr. Charles B. OwenProgramming in C21

Page 22: 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

Pointers to structs

typedef struct{int width;

Box b; /* A variable of type Box */Box *c;/* A pointer to a Box */double w;int length;

int height;} 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 a variable of the struct’s type‐> for a pointer to a struct

CSE 251 Dr. Charles B. OwenProgramming in C22

> for a pointer to a struct

Page 23: 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

struct Concepts struct Box b;  /* No typedef */Circle c; /* typedef */

struct Box{

double wid, hit;}

struct Box *pBox; /* Pointer to Box */Circle *pCirc; /* Pointer to Circle */

};

typedef struct{

pBox = &b; /* Get pointer to a Box */b.wid = 3;pBox‐>wid = 7;{

double radius;int x;int y;char name[10];

pBox >wid  7;

pCirc = &c;(*pCirc).radius = 9;char name[10];

} Circle;

CSE 251 Dr. Charles B. OwenProgramming in C23 1


Recommended