strings, arrays, and pointers

43
Strings, Arrays, and Pointers CS-2301, B-Term 20 09 1 Strings, Arrays, and Pointers CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Upload: holland

Post on 09-Feb-2016

62 views

Category:

Documents


0 download

DESCRIPTION

Strings, Arrays, and Pointers. CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language , 2 nd edition, by Kernighan and Ritchie and from C: How to Program , 5 th and 6 th editions, by Deitel and Deitel). Reading Assignment. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 1

Strings, Arrays, and Pointers

CS-2301, System Programmingfor Non-Majors

(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)

Page 2: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 2

Reading Assignment

• Kernighan & Ritchie, Chapter 5– All the way through!

• Study §5.5 in detail — pages 104-107– Character pointer and functions– You will use this all the time!

• Study §5.10 — pages 114-118– Command line arguments– You will use this a lot!

Page 3: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 3

Review

• Array – a set of elements all of the same type stored contiguously in memory – e.g.,– int A[25]; // 25 integers– struct Str B[15]; /* 15 objects of

type struct Str */– double C[]; /* indeterminate #

of doubles */

• Pointer – a variable whose value is the location of some other object– float *p; // pointer to a float

Page 4: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 4

Review (continued)

int A[25]; // 25 integers

• Type of A[i] is int (for all i).

• Type of A is int *• I.e., pointer to int

Page 5: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 5

Review (continued)

• '&' operator generates a pointer to its operand – e.g., – int n;– Type of &n is int *

• Unary '*' operator dereferences a pointer –i.e., accesses the thing pointed to – e.g., – int *p;– Type of *p is int

Page 6: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 6

Review (continued)

• Arrays and pointers are closely related• Let int A[25];

int *p; int i, j;• Let p = A;• Then p points to A[0]

p + i points to A[i]&A[j] == p+j*(p+j) is the same as A[j]

Page 7: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 7

Review (continued)

• If void f(int A[], int arraySize);

• Then f(&B[i], bSize-i) calls f with subarray of B as argument

• Starting at element i, continuing for bSize-i elements

Page 8: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 8

Review (concluded)

void f(int A[], int arraySize);and

• void f(int *A, int arraySize);are identical!

• Most C programmers use pointer notation rather than array notation

• In these kinds of situations

Page 9: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 9

Additional Notes

• A pointer is not an integer …• Not necessarily the same size• May not be assigned to each other

• … except for value of zero!• Called NULL in C; defined in <stdio.h>• Means “pointer to nowhere”

• void * is a pointer to no type at all• May be assigned to any pointer type• Any pointer type may be assigned to void *

Page 10: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 10

Additional Notes

• A pointer is not an integer …• Not necessarily the same size• May not be assigned to each other

• … except for value of zero!• Called NULL in C; defined in <stdio.h>• Means “pointer to nowhere”

• void * is a pointer to no type at all• May be assigned to any pointer type• Any pointer type may be assigned to void *

Defeats type-checking in the

compiler

A easy way to get into big

trouble

Absolutely necessary in most

large C programs

Page 11: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 11

Questions?

Page 12: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 12

Characters in C

• char is a one-byte data type capable of holding a character

• Treated as an arithmetic integer type• (Usually) unsigned

• May be used in arithmetic expressions• Add, subtract, multiply, divide, etc.

• Character constants• 'a', 'b', 'c', …'z', '0', '1', … '9', '+', '-', '=', '!', '~', etc, '\n', '\t', '\0', etc.

• A-Z, a-z, 0-9 are in order, so that arithmetic can be done

Page 13: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 13

Strings in C

• Definition:– A string is a character array ending in '\0' — i.e.,– char s[256];– char t[] = "This is an initialized string!";– char *u = "This is another string!";

• String constants are in double quotes• May contain any characters• Including \" and \' — see p. 38, 193

• String constants may not span lines• However, they may be concatenated — e.g., "Hello, " "World!\n" is the same as "Hello, World!\n"

Page 14: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 14

Strings in C (continued)

• Let– char *u = "This is another string!";

• Then– u[0] == 'T'u[1] == 'h'u[2] == 'i'…u[21] == 'g'u[22] == '!'u[23] == '\0'

Page 15: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 15

Support for Strings in C

• Most string manipulation is done through functions in <string.h>

• String functions depend upon final '\0'• So you don’t have to count the characters!

• Examples:–– int strlen(char *s) – returns length of string

• Excluding final '\0'– char* strcpy(char *s, char *ct) – Copies string ct to

string s, return s• s must be big enough to hold contents of ct• ct may be smaller than s

– See pp. 105-106 for various implementations

Page 16: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 16

Support for Strings in C (continued)

• Examples (continued):–– int strcmp(char *s, char *t)

• lexically compares s and t, returns <0 if s < t, >0 ifs > t, zero if s and t are identical (p. 106)

– char* strcat(char *s, char *ct)• Concatenates string ct to onto end of string s, returns s•s must be big enough to hold contents of both strings!

• Other string functions– strchr(), strrchr(), strspn(), strcspn() strpbrk(), strstr(), strtok(), …

– pp. 249-250

Page 17: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 17

Character functions in C

• See <ctype.h>, p. 248-249• These return or false (0) or true (non-zero)int isdigit(int c) int isalpha(int c)int isalnum(int c) int isxdigit(int c)int islower(int c) int isupper(int c)int isspace(int c) int iscntrl(int c)int ispunct(int c) int isprint(int c)int isgraph(int c)• These change case (if appropriate)int toupper(int c) int tolower(int c)

Page 18: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 18

String Conversion Functions in C

• See <stdlib.h>, p. 251-252double atof(const char *s)int atoi(const char *s)long atol(const char *s)

double strtod(const char *s, char **endp)long strtol(const char *s, char **endp, int base)

unsigned long strtoul(const char *s, char **endp, int base)

Page 19: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 19

Dilemma

• Question:–– If strings are arrays of characters, …– and if arrays cannot be returned from functions,

– how can we manipulate variable length strings and pass them around our programs?

• Answer:–– Use storage allocated in The Heap!

Page 20: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 20

Definition — The Heap

• A region of memory provided by most operating systems for allocating storage not in Last in, First out discipline

• I.e., not a stack

• Must be explicitly allocated and released• May be accessed only with pointers

• Remember, an array is equivalent to a pointer

• Many hazards to the C programmer

Page 21: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 21

Static Data Allocation

0x00000000

0xFFFFFFFF

address space

program code(text)

static data

heap(dynamically allocated)

stack(dynamically allocated)

PC

SPThis is The Heap.

Page 22: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 22

Allocating Memory in The Heap

• See <stdlib.h>, p. 251-252void *malloc(size_t size);void free(void *ptr);void *calloc(size_t nmemb, size_t size);void *realloc(void *ptr, size_t size);

• malloc() — allocates size bytes of memory from the heap and returns a pointer to it.

• NULL pointer if allocation fails for any reason• free() — returns the chunk of memory pointed

to by ptr• Must have been allocated by malloc or calloc

Page 23: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 23

Allocating Memory in The Heap

• See <stdlib.h>, p. 251-252void *malloc(size_t size);void free(void *ptr);void *calloc(size_t nmemb, size_t size);void *realloc(void *ptr, size_t size);

• malloc() — allocates size bytes of memory from the heap and returns a pointer to it.

• NULL pointer if allocation fails for any reason• free() — returns the chunk of memory pointed

to by ptr• Must have been allocated by malloc or calloc

Segmentation fault and/or big-

time error if bad pointer

Page 24: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 24

Allocating Memory in The Heap

• See <stdlib.h>, p. 251-252void *malloc(size_t size);void free(void *ptr);void *calloc(size_t nmemb, size_t size);void *realloc(void *ptr, size_t size);

• malloc() — allocates size bytes of memory from the heap and returns a pointer to it.

• NULL pointer if allocation fails for any reason• free() — returns the chunk of memory pointed

to by ptr• Must have been allocated by malloc or calloc

free() knows size

of chunk

allocated by malloc(

) or

calloc()

Page 25: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 25

Notes

• calloc() is just a variant of malloc()• malloc() is analogous to new in C++ and

Java•new in C++ actually calls malloc()

• free() is analogous to delete in C++•delete in C++ actually calls free()• Java does not have delete — uses garbage

collection to recover memory no longer in use

Page 26: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 26

Typical usage of malloc() and free()

char *getTextFromSomewhere(…);

int main free(){char * txt;…;txt = getTextFromSomewhere(…);…;printf("The text returned is %s.", txt);free(txt);

}

Page 27: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 27

Typical usage of malloc() and free()

char * getTextFromSomewhere(…){char *t;...t = malloc(stringLength);...return t;

}

int main free(){char * txt;…;txt = getTextFromSomewhere(…);…;printf("The text returned is %s.", txt);free(txt);

}

getTextFromSomewhere() creates a new string using malloc()

Page 28: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 28

Typical usage of malloc() and free()

char * getTextFromSomewhere(…){char *t;...t = malloc(stringLength);...return t;

}

int main free(){char * txt;…;txt = getTextFromSomewhere(…);…;printf("The text returned is %s.", txt);free(txt);

}

Pointer to text is assigned to

txt in calling function

Page 29: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 29

Usage of malloc() and free()

char *getText(…){char *t;...t = malloc(stringLength);...return t;

}

int main free(){char * txt;…;txt = getText(…);…;printf("The text returned is %s.", txt);free(txt);

}

main() must remember to

free the storage pointed to

by txt

Page 30: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 30

Definition – Memory Leak

• The steady loss of available memory due to forgetting to free() everything that was malloc’ed.

• Bug-a-boo of most large C programs

• If you “forget” the value of a pointer to a piece of malloc’ed memory, there is no way to find it again!

• Killing the program frees all memory!

Page 31: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 31

Questions?

Page 32: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 32

String Manipulation in C

• Almost all C programs that manipulate text do so with malloc’ed and free’d memory

• No limit on size of string in C• Need to be aware of sizes of character

arrays!• Need to remember to free storage when it is

no longer needed• Before forgetting pointer to that storage!

Page 33: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 33

Input-Output Functions

• printf(const char *format, ...)• Format string may contain %s – inserts a string

argument (i.e., char *) up to trailing '\0'• scanf(const char *format, ...)

• Format string may contain %s – scans a string into argument (i.e., char *) up to next “white space”

• Adds '\0'• Related functions

•fprintf(), fscanf() – to/from a file•sprintf(), sscanf() – to/from a string

Page 34: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 34

Example Hazard

char word[20];…;scanf("%s", word);

• scanf will continue to scan characters from input until a space, tab, new-line, or EOF is detected

• An unbounded amount of input• May overflow allocated character array• Probable corruption of data!• scanf adds trailing '\0'

• Solution:–scanf("%19s", word);

Page 35: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 35

Questions?

Programming Assignment #4

Page 36: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 36

Command Line Arguments

• See §5.10• By convention, main takes two arguments:-

int main(int argc, char *argv[]);– argc is number of arguments– argv[0] is string name of program itself– argv[i] is argument i in string form

• i.e., i < argc– argv[argc] is null pointer!

• Sometimes you will see (the equivalent)

int main(int argc, char **argv);

An array of pointers to

char (i.e., of strings)

Page 37: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 37

Example — PA #4

• Instead of prompting user for # of items to read, simply take it from command line% statistics 1000

would create an array of 1000 items, play 1000 games and store the results of those games in the array.

argv[0] – name of program

Page 38: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 38

Example — PA #4 (continued)

• Instead of prompting user for # of items to read, simply take it from command line% statistics 1000

would create an array of 1000 items, play 1000 games and store the results of those games in the array.

argv[1] – first program

argument

Page 39: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 39

Example — PA #4 (continued)

int main(int argc, char *argv[])){int nElements;double array[];if (argc <= 1) {

printf(“Enter number of games:- ");scanf(“%d”, &nElements);

} else nElements = atoi(argv[1]);

array = calloc(nElements, sizeof double);

/* rest of program using array[i] */

free(array);return 0;

} // main(argc, argv)

Page 40: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 40

Example — PA #4 (continued)

int main(int argc, char *argv[])){int nElements;double array[];if (argc <= 1) {

printf(“Enter number of games:- ");scanf(“%d”, &nElements);

} else nElements = atoi(argv[1]);

array = calloc(nElements, sizeof double);

/* rest of program using array[i] */

free(array);return 0;

} // main(argc, argv)

array of unknown size declared; no storage allocated

Page 41: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 41

Example — PA #4 (continued)

int main(int argc, char *argv[])){int nElements;double array[];if (argc <= 1) {

printf(“Enter number of games:- ");scanf(“%d”, &nElements);

} else nElements = atoi(argv[1]);

array = calloc(nElements, sizeof double);

/* rest of program using array[i] */

free(array);return 0;

} // main(argc, argv)

Convert argument #1 to int for # of elements

Allocate nElements of double,

store pointer in array

Page 42: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 42

Example — PA #4 (continued)

int main(int argc, char *argv[])){int nElements;double array[];if (argc <= 1) {

printf(“Enter number of games:- ");scanf(“%d”, &nElements);

} else nElements = atoi(argv[1]);

array = calloc(nElements, sizeof double);

/* rest of program using array[i] */

free(array);return 0;

} // main(argc, argv)

Be sure to free() the allocated array before exiting!

Page 43: Strings, Arrays, and Pointers

Strings, Arrays, and Pointers

CS-2301, B-Term 2009 43

Questions?