Docsity

Midterm Exam - Introduction to Low - Level Programming | CMSC 212, Exams of Computer Science

Partial preview of the text

Download Midterm Exam - Introduction to Low - Level Programming | CMSC 212 and more Exams Computer Science in PDF only on Docsity!

Announcements

z^

Program #1B– Due on Thursday

z^

Midterm #1– next Tuesday (ARM 0131)– cover through today's lecture

z^

Reading– Chapter 8, 9 (Today)

  • skip 8.1.4• skip 9.
    • Chapter 6 (Thursday)

Pointer Arithmetic

z^

Pointers are first class types– arithmetic applies to pointers just like ints.– This is useful, but can get tricky

z^

Adding pointer and int– Adds n objects to value pointed to.

  • If objects are 100 bytes, p+1 is 100 bytes later in memory
    • Example:

int x = 5;int a[10];int p = a;(p + 3) = 42;

/* updates a[3] to 42 */

z^

[] is really shorthand for pointer addition– a[n] is really the same as *(a + n)

Multidimensional Arrays

z^

int matrix[3][4];– C uses arrays of arrays to handle multidimensional arrays– This is an array of 3 elements, each containing 4 integers

z^

Storage Order– row major order– right most dimension has adjacent elements in consecutive

memory

z^

Subscripts– matrix+

is the second group of 4 int elements

Pointers To Arrays

z^

int vector[10], *vp=vector;– vp points to the array of 10 integers

z^

int matrix[3][10], *mp = matrix;– ERROR: mp points to an array of ints, but matrix is an array

of arrays

z^

Can also treat multi-dimensional arrays as single– int *pi = &matrix[0][0]– int *pi = matrix[0]– advancing pi (pi++) will navigate entire array of arrays

Initialization

z^

Two Possible Approaches

z^

int matrix[2][3] = { 100, 101, 201, 202, 301, 302 };– as one long array

z^

int matrix[2][3] = {

{ 100, 101 , 201 } , { 202 , 301, 302 } };– as array of arrays

z^

Second approach is strongly preferred

Arrays of Pointers

z^

int *api[10];– An array of 10 pointers to integers;

char *keyword[] = {

"Load","Store","Negate",…

Strings

z^

Zero or more characters followed by NUL– not counted as part of string– string.h defines prototypes for string routines

z^

Copying Strings– size_t strlen(char const *str);

  • returns count of characters in str
    • char *strncpy(char *dst, char const *src, size_t len);
      • copy src to dst• copy until NUL in src or at most len characters• pad extra characters will NUL• Safety tip:

dst[len-1] = '\0'; to force termination

  • char *strncat(char *dst, char const *src, size_t len);
    • append src onto the end of dst• always appends NUL ('\0') to end of string

String Functions

z^

Comparison– int strncmp(char const *s1, char const s2, size_t len);

  • returns 0 if string equal up to len• returns -1 if s1 is less than s2• returns 1 if s1 is greater than s

z^

Searching– char *strchr(char const *str, int ch);– char *strrchr(char const *str, int ch);

  • finds the first occurance of ch in str• strrchr finds the last occurance• returns NULL if not found
    • char *strstr(char const *s1, char const *s2);
      • find the first occurrence of s2 in s

Character Functions

z^

Prototypes in ctype.h

z^

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), isupper(int ch)
          • return true if it’s a-z, A-Z
            • isalpha(int ch)
              • returns true if it's a-z or A-Z

z^

Transformation– int toupper(int ch), int tolower(int ch)

  • converts to upper/lower case

Blocks of Memory

z^

C supports operations on blocks of memory– might be an array, a struct, an array of structs, etc.

z^

Copying– void *memcpy(void *dest, const void *src, size_t len);

  • copies memory pointed to by src to memory pointed to by dest - copies len bytes of memory• returns the value of src -^

void *memmove(void *dest, const void *src, size_t len);• copies memory pointed to by src to memory pointed to by

dest

  • copies len bytes of memory• returns the value of src• handles the case if dest and src overlap

Examples of memory functions

char buffer[25];memset(buffer, '\0', sizeof(buffer));struct foo {

int a;char b;

} dest, src;memcpy(&dest, &src, sizeof(dest));