30
C Slides and captured lecture (video and sound) are available at: www.cim.mcgill.ca/~jer/C/

Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

  • Upload
    kedma

  • View
    25

  • Download
    0

Embed Size (px)

DESCRIPTION

C. Slides and captured lecture (video and sound) are available at: www.cim.mcgill.ca/~jer/C/. Crash Course -- Review. Last week, we covered: variable declarations and assignments float x;double d = 5;int *p, i, a[100];char s[21]; y = z = 4 + 5; x += 1; ++x; y = x--; formatted I/O - PowerPoint PPT Presentation

Citation preview

Page 1: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

CSlides and captured lecture (video and sound) are available at:

www.cim.mcgill.ca/~jer/C/

Page 2: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Crash Course -- Review

Last week, we covered:•variable declarations and assignments

float x;double d = 5;int *p, i, a[100];char s[21]; y = z = 4 + 5; x += 1; ++x; y = x--;

•formatted I/Oint snum; float gr; char mark; char name[80];scanf ("%d %f %c %s", &snum, &gr, &mark, name); printf (”%s\’s mark is %f or %c\n”,name, mark, gr);

•loopswhile (1) printf ("hi");for (j = 0; j * j <= up; j++) printf ("%d \n", j * j);

Page 3: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Exercise: Caesar's code

•Input: sequence of capital letters•Output: another sequence of capital letters obtained by

shifting each letter in the original sequence three places in the alphabet. Note: shifting wraps around.

•Example: KENNEDY -> NHQQHGB•recall the use of this handy string function:

int strlen(char *);

example:n = strlen ("there"); /* n is assigned 5 */

Page 4: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Crash Course -- Part II

Today, we will cover:•functions•types•pointers•arrays•structures•using linked lists•simple debugging

Page 5: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Functions/* Increment; takes an integer argument and * returns the argument plus one. */ int incr (int i){ int j; j = i + 1; return j;}

main () { int k, m = 4; k = incr(m); printf ("k = %d, m = %d\n", k, m);}

•output: k = 5, m = 4

Page 6: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

More about Functions•might have no return type, and no return statement:

void printhi (){ printf ("hi\n");}

•parameters are copied and can be modifiedint incr (int i){ i++; return i;}

•default (unspecified) return type is int

Page 7: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Variables within Functions

But this does not work:void no_incr (int i){ i++;}

void main () { int x = 5; no_incr(x); printf ("%d\n", x);}

•beware that modifications are on internal copies of the parameters.

•note: void main() since main does not return a value

Page 8: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Exercise

Write a function that checks whether a sentence has 'title case':

•Arguments: sentence string•Assume the string consists of letters and blanks only.•Return true iff each word in the sentence starts with a

capital letter and continues with lowercase letters.

Page 9: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

C Preprocessor Directives

Operations before compilation phase:•include "header" files that contain pre-supplied functions:

#include <string.h>•this line will be substituted by the contents of the file•define symbolic constants:

#define MY_LARGE_NUMBER 12345#define MY_NIFTY_STRING "abc"

Page 10: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Make your own booleans

C has boolean operations on int:== != && || !

However, C has no boolean type. Make it!#define boolean_t int;#define TRUE 1#define FALSE 0

Example:boolean_t xIsFive, zAsBig, result;xIsFive = (x == 5);zAsBig = (z >= x);if (xIsFive || zAsBig) { result = TRUE;}

Page 11: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Naming your own type

A better way: use typedeftypedef int boolean_t;

Syntax:typedef existing-type new-type;

Comments:•typedef does not create a new type•only creates a new label for an existing type•makes your code easier to read

Page 12: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

typedef in action

Example:typedef char * string_t;typedef int boolean_t;

string_t msg = "No more room";boolean_t full_class;

if (num_students >= MAX_SIZE) full_class= TRUE;

Page 13: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

i == *pi

Pointers

C relies heavily on pointers:

int i;int *pi;

pi = &i; *pi = 3;

3

pi

Page 14: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Pointers and Arrays

An array name by itself is a pointer to the first element:

int a[100]; *a = 5; /* same as a[0] = 5 */*(a + 3) = 10; /* same as a[3] = 10; *//* Note: *a + 3 means something else */

Strings are arrays of char:char s[3];strcpy (s, "hi"); /* same as following line */s[0] = 'h'; s[1] = 'i'; s[2] = '\0';

Page 15: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

4j

i

Call by referencevoid incr (int *i);/* * increment i by 1 */{ *i = *i + 1;}

main(){ int j;

j = 4; incr (&j); printf ("j = %d\n", j);

}•&j and i are pointers to the same integer: incr can change the

integer (side effect), but not the pointer

Page 16: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Dynamic Memory Allocation

Pointer errors can be nastyvoid main () { int *p; *p = 5;}

Reserve space for the "pointed to" object:#include <stdlib.h>void main () { int *p; p = (int *)malloc (sizeof (int)); *p = 5; printf ("value at p = %d\n", *p); free (p); /* must do this yourself! */}

Page 17: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Finding Bugs

Insert "print" statements:printf ("At point A, %s = %d\n", "x", x);

Use a debugger to help you catch bugs in the act:

•run step-by-step•insert breakpoints•display variables

Page 18: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors I#include <stdio.h>

void main (){ int i;

scanf ("%d", &i); if (i = 0) puts ("false"); else puts ("true");}

What’s wrong?

Page 19: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors I#include <stdio.h>void main (){ int i;

scanf ("%d", &i); if (i = 0) puts ("false"); else puts ("true");}

The assignment, i = 0, will return a value of 0, so the program will always output "true"Use: if(0 == i) to be safe

Page 20: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors II#include <stdio.h>

void main (){ int i;

/* echo one number */ scanf ("%d", i); printf ("input = %d\n", i);}

What’s wrong?Hint: Segmentation fault / Bus error

Page 21: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors II#include <stdio.h>

void main (){ int i;

/* echo one number */ scanf ("%d", i); printf ("input = %d\n", i);}

Must provide the address of an integer if the function is going to assign a value to it.

Page 22: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors III#include <stdio.h>

void main (){ int *pc;

scanf ("%d", pc); printf ("%d\n", pc);}

What’s wrong?

Page 23: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors III#include <stdio.h>

void main (){ int *pc;

scanf ("%d", pc); printf ("%d\n", pc);}

*pc is an uninitialized pointer so results are unpredictable --> use malloc

Page 24: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors IV#include <stdlib.h>

void main (){ double *p;

p = malloc (sizeof (double *));}

What’s wrong?

Page 25: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors IV#include <stdlib.h>

void main (){ double *p;

p = malloc (sizeof (double *));}

Insufficient memory allocated for a double (typically, 8 bytes)Use malloc (sizeof (double));

Page 26: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors V#include <stdio.h>

void main (){ char s[] = "hi";

if (s == "hi") puts ("Strings are equal"); else puts ("Strings are not equal");}

What happens?

Page 27: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Programming Errors V#include <stdio.h>

main (){ char s[] = "hi";

if (s == "hi") puts ("Strings are equal"); else puts ("Strings are not equal");}

Use strcmp() to compare strings, otherwise you are comparing addresses.

Page 28: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Structures

Use struct to create or define a new type:struct str_data { char *string; int length;};struct str_data s1, s2;

Syntaxstruct structure_name {type1 member1;type2 member2;…};

A member of a structure is referred to by:structure_name.member

Page 29: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Structures, cont.

Example:struct str_data { char *string; int length;};struct str_data s1, s2;

s1.string = (char *) malloc(80);

strcpy (s1.string, "How now brown cow.");s1.length = strlen(s1.string);s2 = s1; /* can copy structures, pass to fcns */

Page 30: Slides and captured lecture (video and sound) are available at: cim.mcgill/~jer/C

Arrays of Structures

Example:#define MAX_STUDENT 100

typedef struct { /* define a new type */ char name[80]; int name_len; int student_number;} student_t;

/* create list of elements of type student_t */student_t class[MAX_STUDENT];

for (i = 0; i < MAX_STUDENT; i++) { gets(class[i].name); class[i].name_len = strlen(class[i].name);}