Strings, Arrays, and Pointers CS-2301 D-term 20091 Strings, Arrays, and Pointers CS-2301 System Programming C-term 2009 (Slides include materials from.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Chapter 10.
Arrays in CCS-2301, B-Term Arrays in C (including a brief introduction to pointers) CS-2301, System Programming for Non-Majors (Slides include materials.
Strings, Arrays, and Pointers CS-2303, C-Term Strings, Arrays, and Pointers CS-2303 System Programming Concepts (Slides include materials from The.
Arrays in C & C++CS-2303, C-Term Arrays in C & C++ (including a brief introduction to pointers) CS-2303 System Programming Concepts (Slides include.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Arrays in CCS-2301 B-term Arrays in C (with a brief Introduction to Pointers) CS-2301, System Programming for Non-majors (Slides include materials.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
Pointers Applications
Strings, Arrays, and Pointers CS-2301, B-Term Strings, Arrays, and Pointers CS-2301, System Programming for Non-Majors (Slides include materials.
CS-2303 System Programming Concepts
Arrays and Pointers in C Alan L. Cox
Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Pointers, Heap Memory Rudra Dutta CSC Spring 2007, Section 001.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Arrays in CCIS 1057 Fall Arrays in C (with a brief Introduction to Pointers) CIS 1057 Computer Programming in C Fall 2013 (Slides include materials.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Dynamic Allocation in C
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Strings, Arrays, and Pointers
Day 03 Introduction to C.
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Memory Allocation CS 217.
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Arrays and Pointers in C & C++
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Differences between Java and C
Strings in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Dynamic Memory – A Review
Presentation transcript:

Strings, Arrays, and Pointers CS-2301 D-term Strings, Arrays, and Pointers CS-2301 System Programming C-term 2009 (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)

Strings, Arrays, and Pointers CS-2301 D-term Reading Assignment Kernighan & Ritchie, Chapter 5 –All the way through! Study §5.5 in detail — pages –Character pointer and functions –You will use this all the time! Study §5.10 — pages –Command line arguments –You will use this a lot!

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term 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]

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term 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 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 *

Strings, Arrays, and Pointers CS-2301 D-term 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 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

Strings, Arrays, and Pointers CS-2301 D-term Questions?

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term 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"

Strings, Arrays, and Pointers CS-2301 D-term 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'

Strings, Arrays, and Pointers CS-2301 D-term Support for Strings in C Most string manipulation is done through functions in 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 for various implementations

Strings, Arrays, and Pointers CS-2301 D-term Support for Strings in C (continued) Examples (continued) :– –int strcmp(char *s, char *t) lexically compares s and t, returns 0 if s > 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

Strings, Arrays, and Pointers CS-2301 D-term Character functions in C See, p 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)

Strings, Arrays, and Pointers CS-2301 D-term String Conversion Functions in C See, p double 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)

Strings, Arrays, and Pointers CS-2301 D-term 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!

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term Static Data Allocation 0x xFFFFFFFF address space program code (text) static data heap (dynamically allocated) stack (dynamically allocated) PC SP This is The Heap.

Strings, Arrays, and Pointers CS-2301 D-term Allocating Memory in The Heap See, p void *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

Strings, Arrays, and Pointers CS-2301 D-term Allocating Memory in The Heap See, p void *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

Strings, Arrays, and Pointers CS-2301 D-term Allocating Memory in The Heap See, p void *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()

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term Typical usage of malloc() and free() char *getText(…); int main free(){ char * txt; …; txt = getText(…); …; printf("The text returned is %s.", txt); free(txt); }

Strings, Arrays, and Pointers CS-2301 D-term Typical 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); } getText() creates a new string using malloc()

Strings, Arrays, and Pointers CS-2301 D-term Typical 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); } Pointer to text is assigned to txt in calling function

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term 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!

Strings, Arrays, and Pointers CS-2301 D-term Questions?

Strings, Arrays, and Pointers CS-2301 D-term 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!

Strings, Arrays, and Pointers CS-2301 D-term 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

Strings, Arrays, and Pointers CS-2301 D-term 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);

Strings, Arrays, and Pointers CS-2301 D-term 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)

Strings, Arrays, and Pointers CS-2301 D-term Example — Lab 4 Instead of prompting user for # of items to read, simply take it from command line % lab4 20 would create an array of 20 items, read in twenty numbers from standard input, and store in the array.

Strings, Arrays, and Pointers CS-2301 D-term Example — Lab 4 Instead of prompting user for # of items to read, simply take it from command line % lab4 20 would create an array of 20 items, read in twenty numbers from standard input, and store in the array. argv[0] – name of program

Strings, Arrays, and Pointers CS-2301 D-term Example — Lab 4 Instead of prompting user for # of items to read, simply take it from command line % lab4 20 would create an array of 20 items, read in twenty numbers from standard input, and store in the array. argv[1] – first program argument

Strings, Arrays, and Pointers CS-2301 D-term Example – Lab 4 (continued) int main(int argc, char *argv[])){ int nElements; double array[]; if (argc <= 1) { printf("Error: must specify " "number of elements in command line.\n"); return -1; } nElements = atoi(argv[1]); array = calloc(nElements, sizeof double); /* rest of program using array[i] */ free(array); return 0; }//main(argc, argv)

Strings, Arrays, and Pointers CS-2301 D-term Example – Lab 4 (continued) int main(int argc, char *argv[])){ int nElements; double array[]; if (argc <= 1) { printf("Error: must specify " "number of elements in command line.\n"); return -1; } 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

Strings, Arrays, and Pointers CS-2301 D-term Example – Lab 4 (continued) int main(int argc, char *argv[])){ int nElements; double array[]; if (argc <= 1) { printf("Error: must specify " "number of elements in command line.\n"); return -1; } 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

Strings, Arrays, and Pointers CS-2301 D-term Example – Lab 4 (continued) int main(int argc, char *argv[])){ int nElements; double array[]; if (argc <= 1) { printf("Error: must specify " "number of elements in command line.\n"); return -1; } nElements = atoi(argv[1]); array = calloc(nElements, sizeof double); /* rest of program using array[i] */ free(array); return 0; }//main(argc, argv) Allocate nElements of double, store pointer in array

Strings, Arrays, and Pointers CS-2301 D-term Example – Lab 4 (continued) int main(int argc, char *argv[])){ int nElements; double array[]; if (argc <= 1) { printf("Error: must specify " "number of elements in command line.\n"); return -1; } 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!

Strings, Arrays, and Pointers CS-2301 D-term Questions?