1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11...

22
07/05/22 Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

description

1/15/2016Course material created by D. Woit 3 Returning a structure to a calling program /*Source: struct6.c*/ #include struct complex { int r; int i; } ; struct complex ReadIt(void); int main(void) { struct complex X; X=ReadIt(); printf("%d + %di ",X.r, X.i); return 0; } struct complex ReadIt() { struct complex temp; scanf("%d %d", &temp.r, &temp.i); return(temp); } data from temp *copied* into storage set aside for X

Transcript of 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11...

Page 1: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 1

CPS 393Introduction to Unix and C

START OF WEEK 11 (C-5)

Page 2: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 2

Functions and Structures • We can pass a structure to a

function: func1(woit);• call by value as always (not

modified)• func1 makes a *copy* of car woit

• /*Source: struct5.c*/• #include <stdio.h>• struct complex {• int r;• int i;• };• void WriteSum(struct complex c1,

struct complex c2);

• int main(void) {• struct complex A1, A2;• A1.r = 4; A1.i = 7;• A2.r = 3; A2.i = -1;• WriteSum(A1,A2);• return 0;• }• • void WriteSum(struct

complex c1, struct complex c2) {

• printf("%d + %di\n", (c1.r)+(c2.r), (c1.i)+(c2.i));

• }

Page 3: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 3

Returning a structure to a calling program• /*Source: struct6.c*/• #include <stdio.h>• struct complex {• int r;• int i;• } ;• struct complex ReadIt(void);• int main(void) {• struct complex X;• X=ReadIt();• printf("%d + %di\n",X.r, X.i);• return 0;• }

• struct complex ReadIt() {• struct complex temp;• scanf("%d %d", &temp.r,

&temp.i);• return(temp);• }

• data from temp *copied* into storage set aside for X

Page 4: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 4

HMWKRewrite the above programs using typedefsfor complex

numbers.• Put functions WriteSum and MakeComplex, (along with

anything else necessary)into a file called cmplx.c that can be compiled separately. Write a header file cmplx.h

• A program similar to this should then work (try it):• #include "cmplx.h"• void main(void) {• complex X,Y;• X=MakeComplex(1,2);• Y=MakeComplex(3,4);• WriteSum(X,Y);• }

Page 5: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 5

Pointers to Structures • In the example below

pointer p point to the structure c1.

• With pointer, structure can be accessed in two ways:

• struct complex {• int r;• int i;• } c1, *p;• p=&c1;• (*p).r = 4; //or• p->r = 4;

• We can pass pointers to structures to functions instead of actual structure

• 1. to change value of structure inside function

• 2. saves execution time since structures could be hundreds of bytes long while a ptr is usually 4 bytes (if just pass structure, a *copy*of it must be made--time consuming)

Page 6: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 6

Example of pointer to structures• Note ReadIt and WriteIt print using different syntax

because • ReadIt has POINTER to complexi, but WriteIt has complex.• /*Source: struct8.c*/• #include <stdio.h>• struct complex {• int r;• int i;• };• void ReadIt(struct complex *c);• void WriteIt(struct complex p);

Page 7: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 7

Struct8.c• int main(void) {• struct complex A1, A2;• ReadIt(&A1); • WriteIt(A1);• ReadIt(&A2);• WriteIt(A2);• return 0;• }

• void ReadIt(struct complex *c) {

• printf("Enter complex: ");• scanf("%d %d",&(c->r),

&(c->i));• printf("Read %d + %di\

n",c->r, c->i);• }• void WriteIt(struct

complex c) {• printf("Write: %d +

%di\n", c.r, c.i);• }

Page 8: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 8

Struct9.c• /*Source: struct9.c*/• #include <stdio.h>• struct complex {• int r;• int i;• };• void ScalarMult(int i, struct complex *p);• void PrintComplex(struct complex *p);• int main(void) {• struct complex C={4,-2};• ScalarMult(3,&C);• PrintComplex(&C);• return 0;• }

Page 9: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 9

Struct9.c• void ScalarMult(int m, struct complex *p) {• //pointer is required since mutating p• p->r = p->r * m;• p->i = p->i * m;• }• void PrintComplex(struct complex *c){• //pointer unnecessary since not mutating p• //printf("%d + %di\n", c->r, c->i);• printf("%d + %di\n", (*c).r, (*c).i);• }

Page 10: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 10

HMWK• rewrite the above program using typedefs

for complex.• Write ConjugateComplex which turns a

complex number into its conjugate (imaginary part multiplied by -1).

• Write a function called NegComplex turns a complex number

• into its negative (both imaginary and real parts multiplied by -1.

Page 11: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 11

C's Time and Date Functions use Structures • use structures to hold info• defined in structure tm in <time.h>• struct tm {• int tm_sec; /*seconds, 0-59*/• int tm_min; /*minutes, 0-59*/• int tm_hour; /*hours, 0-23*/• int tm_mday; /*day of month, 0-31*/• int tm_mon; /*months since Jan, 0-11*/• int tm_year; /*years since 1900*/• int tm_wday; /*days since Sunday 0-6*/• int tm_yday; /*days since Jan 1, 0-365*/• int tm_isdst; /*daylight savings time flag*/• };• struct tm *localtime(time_t *time);• time_t time(time_t *time1);

Page 12: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 12

Example tm1.c• /*Source: tm1.c• Purpose: to display

current system time and date

• */• #include <stdio.h>• #include <time.h>• void main(void) {• struct tm *systime;• time_t t;

• t = time(NULL);• systime = localtime(&t);• printf("Time is %d:%d:%d\

n",• systime->tm_hour,

systime->tm_min,• systime->tm_sec);• printf("Date is %d/%d/%d\

n",• systime->tm_mon+1,

systime->tm_mday,• systime-

>tm_year+1900);• }

Page 13: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 13

System structures• C has many other structures to give

programmer access to system information. e.g., dirent.h lets you access the

• linux filesystem (files, dirs, their perms, etc.)

Page 14: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 14

Nested Structures • struct evaluation {• int tests; /*number of

tests*/• int exam; /*non-0=yes,

0=no*/• int assigns; /*number of*/• int labs; /*number of*/• };

• struct class {• char CourseNum[10];

/*e.g., CPS393 */• char CourseName[80];

/*e.g., Intro to C & UNIX */• struct evaluation eval;

/*eval info*/• } c1, c2[10];

• c1.eval.assigns = 3;• c2[0].eval.labs=6;

Page 15: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 15

Dynamic Memory Allocation • In C we can allocate and free storage at run-time (DMA)• v.s. compile-time as we have done so far• calloc(NumItems, ItemSize);• NumItems: number of items of storage requested• ItemSize: size of each item (in bytes)• calloc returns void (generic) pointers.• must *cast* to desired type • (ptr to NumItems x ItemSize bytes of memory)• storage is initialized to zeros

Page 16: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 16

Dynamic Memory Allocation

• malloc(Size);• Size: number of bytes• storage not initialized• e.g., to reserve an area for n integers

Page 17: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23Course material created by D. Woit 17

• /*source: trycalloc.c */• #include <stdio.h>• #include <stdlib.h>• int main(void) {• int n; /*num elts of future

array*/• int *p, *tmp; /*pointers to the

future array*/• scanf("%d",&n);• int i;• p = (int *) calloc (n, sizeof(int));• if (p == NULL) {• perror("calloc");• exit(0);}• tmp = p;• /* tmp is used to point to

elements while p point to the beginning of the array */

• /*load array with 0,1,2,...(n-1) */• for (i=0; i<n; i++)• *tmp++ = i;• //tmp[i]=i; //same• for (i=0; i<n; i++)• printf("p[%d] is %d\n",i,p[i]);• /* or• tmp = p;• for (i=0; i<n; i++)• printf("p[%d] is %d\n",i,*tmp+

+);• */• exit(1);• }

Page 18: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 18

Releasing the memory• When memory no longer needed, free it for later use.• When pgm ends, all dynamically allocated memory is

freed, but if no longer needed *during* pgm execution, could free it immediately to release storage

• A long-running program, like the linux operating system itself must always free its memory, or eventually lots of unused, but unavailable memory around (full kidneys)

• free(p);

• Can dynamically allocate strings similarly.• Since char is always 1 byte didn't do sizeof(char)

Page 19: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 19

Stralloc.c

• /*source: stralloc.c*/• #include <stdio.h>• #include <stdlib.h>• #define MAX 10• int main(void){• char *A;• int max_int=0;

• //need to add error-checking• printf("enter max string length:

");• scanf("%d",&max_int);• while ((getchar())!='\n');• A=(char

*)malloc(max_int+1); //room for null char

• printf("enter string: ");• fgets(A,max_int,stdin);• printf("Third char is: %c\

n",*(A+2));• exit(0);• }

Page 20: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 20

HMWK• read about malloc and calloc (text, man pages).• What do they return if no storage left to allocate• (i.e., if they fail)?• Write a program that reads an input file such as:• 4• 1 5• 2 -4• -3 9• 8 2• The first integer, call it n, is the number of • complex numbers to follow. The n complex numbers• are then listed. Your program must dynamically• allocate enough memory to store the array of n• complex numbers.

Page 21: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 21

HMWK cont• Complex numbers must be stored• in a structure. (So you have an array of • structures.) Then your program should loop through• the array from end to front, and print out the• complex numbers in it. E.g., your output should• look like• 8 + 2i• -3 + 9i• 2 + -4i• 1 + 5i• on stdout.

Page 22: 1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)

05/03/23 Course material created by D. Woit 22