Transcript week5_C

CPS 393
Introduction to Unix and C
START OF WEEK 11 (C-5)
4/2/2016
Course material created by D.
Woit
1
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);
4/2/2016
• 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));
• }
Course material created by D.
Woit
2
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;
}
4/2/2016
• 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
Course material created by D.
Woit
3
HMWK
Rewrite 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);
• }
4/2/2016
Course material created by D.
Woit
4
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;
4/2/2016
• 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)
Course material created by D.
Woit
5
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);
4/2/2016
Course material created by D.
Woit
6
Struct8.c
• int main(void) {
• struct complex A1, A2;
• ReadIt(&A1);
• WriteIt(A1);
• ReadIt(&A2);
• WriteIt(A2);
• return 0;
• }
4/2/2016
• 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);
• }
Course material created by D.
Woit
7
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;
}
4/2/2016
Course material created by D.
Woit
8
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);
}
4/2/2016
Course material created by D.
Woit
9
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.
4/2/2016
Course material created by D.
Woit
10
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);
4/2/2016
Course material created by D.
Woit
11
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;
•
•
•
•
•
•
•
•
•
4/2/2016
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);
}
Course material created by D.
Woit
12
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.)
4/2/2016
Course material created by D.
Woit
13
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;
4/2/2016
Course material created by D.
Woit
14
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
4/2/2016
Course material created by D.
Woit
15
Dynamic Memory Allocation
• malloc(Size);
• Size: number of bytes
• storage not initialized
• e.g., to reserve an area for n integers
4/2/2016
Course material created by D.
Woit
16
•
•
•
•
•
•
•
•
•
•
•
•
•
•
/*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 */
4/2/2016
•
•
•
•
•
•
•
•
•
•
/*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);
• }
Course material created by D.
Woit
17
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)
4/2/2016
Course material created by D.
Woit
18
Stralloc.c
•
•
•
•
•
•
•
/*source: stralloc.c*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
char *A;
int max_int=0;
4/2/2016
• //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);
• }
Course material created by D.
Woit
19
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
15
2 -4
-3 9
82
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.
4/2/2016
Course material created by D.
Woit
20
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.
4/2/2016
Course material created by D.
Woit
21
4/2/2016
Course material created by D.
Woit
22