Docsity

CMSC 212 Lecture 4: Switch Statements, Pointers, Functions, and C-Strings - Prof. Jeffrey , Study notes of Computer Science

Toggle sidebar
bg1
1
1
1
CMSC 212 โ€“ S07 (lect 4)
Announcements
๎˜
Program #1
โ€“ Due February 13
th
(not the 10
th
as stated on the project
description)
๎˜
Reading
โ€“ Chapter 7 & 8
โ€ข skip 8.1.4
โ€“ Chapter 9 & 10 (Thursday)
2
CMSC 212 โ€“ S07 (lect 4)
Switch Statement
switch (expression) {
case constant-expression:
statement;
break;
โ€ฆ.
default:
statement;
break;
}
๎˜
Execution starts at the constant-expression equal to
the expression
โ€“ Without the break, will continue to next statement!!!
๎˜
(expression) assumed to be an integer expression
๎˜
Default constant expression matches anything else
pf3
pf4
pf5

Partial preview of the text

Download CMSC 212 Lecture 4: Switch Statements, Pointers, Functions, and C-Strings - Prof. Jeffrey and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 โ€“ S07 (lect 4)^1

Announcements

 Program

  • Due February 13th^ (not the 10th^ as stated on the project description)

 Reading

  • Chapter 7 & 8
    • skip 8.1.
  • Chapter 9 & 10 (Thursday)

CMSC 212 โ€“ S07 (lect 4)^2

Switch Statement

switch (expression) { case constant-expression: statement; break; โ€ฆ. default: statement; break; }

 Execution starts at the constant-expression equal to

the expression

  • Without the break, will continue to next statement!!!

 (expression) assumed to be an integer expression

 Default constant expression matches anything else

CMSC 212 โ€“ S07 (lect 4)^3

Pointer Variables

 Hold an address of something else

  • its location in memory
  • usually an address to a specified type of space

 type * varname;

  • int * a;
  • char * b;

 Star does not distribute to other variable names

  • int *a, b;
  • int *x, *y;

 Get an address by using a &

  • int a = 6;
  • printf(โ€œ%d is at %p\nโ€, a, &a);

 Dereference also using a star

  • int a, *b;
  • a = 7;
  • b = &a;
  • printf(โ€œ%d and %d \nโ€, a, *b);

CMSC 212 โ€“ S07 (lect 4)^4

Functions

 type name (parameters) block

  • type: return type for the function
  • name: name of the function
  • parameters: a comma separated list of types and names
  • block: the code to run when the function is called

 return; or return expression;

  • terminates a function at that point
  • expression is the value returned by the function
  • implicit termination

CMSC 212 โ€“ S07 (lect 4)^7

Passing Parameters to Main

 int main(int argc, char *argv[])

  • Normal declaration of main
  • argc โ€“ number of command line arguments
    • Includes command itself
  • argv โ€“ array of character strings of command arguments

 Example

  • From command line: % myprog โ€“file myfile
  • In the program:
    • argc == 3
    • argv[0] = โ€œmyprogโ€
    • argv[1] = โ€œ-fileโ€
    • argv[2] = โ€œmyfileโ€

CMSC 212 โ€“ S07 (lect 4)^8

Standard Type Definitions

 Several Types are defined in C's libraries

  • Defined in stddef.h
  • size_t - unsigned integer value (often int or long)
  • ptrdiff_t - signed value (used when subtracting pointers)

 Used by several standard routines

  • sizeof() returns a size_t for example

CMSC 212 โ€“ S07 (lect 4)^9

C-Strings

 Definition

  • An array of characters
  • Where the used portion is terminated by a null character

 <string.h>

  • Library that acts on C-strings
  • Most will crash if given something that does not fit the definition above

 Creating and Initializing a string

char name1[4] = {โ€˜Jโ€™,โ€™aโ€™,โ€™nโ€™,โ€™\0โ€™};

char name2[6] = โ€œPlaneโ€;

 length of the string and the sizeof operator

  • sizeof operator tells the size of the variable or type
  • strlen uses the definition of C-string to find number of used characters

CMSC 212 โ€“ S07 (lect 4)^10

Strings

 Zero or more characters followed by null char โ€˜\0โ€™

  • also called NUL
  • not counted as part of string
  • string.h defines prototypes for string routines

 Copying Strings

  • size_t strlen(char const *str);
    • returns count of characters in str
    • up to but not including the null character
  • char *strncpy(char *dst, char const *src, size_t len);
    • copy src to dst
    • copy until โ€˜\0โ€™ in src or at most len characters
    • pad extra characters will โ€˜\0โ€™
    • Safety tip: dst[len-1] = '\0'; to force termination of new string
  • char *strncat(char *dst, char const *src, size_t len);
    • append src onto the end of dst
    • always appends NUL to end of dst string

CMSC 212 โ€“ S07 (lect 4)^13

Character Functions

 Prototypes in ctype.h

 Classifying characters

  • parameter is int, but it's a character
  • int isspace(int ch);
    • returns true if ch ' ', '\n', '\t', form feed, or carriage return
  • int isdigit(int ch);
    • returns true if its 0 through 9
  • int islower(int ch); and isupper(int ch);
    • return true if itโ€™s a-z for islower and A-Z and isupper
  • int isalpha(int ch);
    • returns true if it's a-z or A-Z
  • int isalnum(int ch);
    • returns true if it's a-z or A-Z or 0-

 Transformation

  • int toupper(int ch), int tolower(int ch)
    • converts to upper/lower case