1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.

Slides:



Advertisements
Similar presentations
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Advertisements

An introduction to pointers in c
1 CS 162 Introduction to Computer Science Chapter 7 Matrix Manipulation Herbert G. Mayer, PSU Status 9/21/2014.
Programming and Data Structure
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.
1 CS 162 Introduction to Computer Science Chapter 9 Binary Trees Herbert G. Mayer, PSU Status 11/23/2014.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Kernighan/Ritchie: Kelley/Pohl:
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Introduction to C Programming CE
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 CS 410 Mastery in Programming Chapter 7 Hints for: Symbolic Differentiation Herbert G. Mayer, PSU CS status 7/26/2011.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Homework / Quiz Exam 2 – Solutions Posted – Questions? Continuing K&R Chapter 6 HW6 is on line – due class 22.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
1 CS 162 Introduction to Computer Science Chapter 5 ASCII to Integer Conversion Herbert G. Mayer, PSU Status 11/9/2014.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Pointers OVERVIEW.
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
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.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Introduction to Data Structures Systems Programming Concepts.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Introduction to Programming 3D Applications CE Lecture 10 Pointers in C.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 CSC103: Introduction to Computer and Programming Lecture No 17.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
1 CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees” Herbert G. Mayer, PSU CS Status 7/9/2014.
Chapter 12 – Data Structures
CS 163 Data Structures Chapter 10 Symbolic Differentiation
Linked List :: Basic Concepts
5.13 Recursion Recursive functions Functions that call themselves
Introduction to Programming
Herbert G. Mayer, PSU CS status 7/29/2013
Pointers and Pointer-Based Strings
CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Introduction to Data Structures
CSCE 210 Data Structures and Algorithms
Programming Languages and Paradigms
Pointers and Pointer-Based Strings
Presentation transcript:

1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014

2 Syllabus Simple int Pointers Simple int Pointers Forward References Forward References Linked List of char Elements Linked List of char Elements

3 Simple int Pointers Define various pointers to integer e.g.: int * p; Define various pointers to integer e.g.: int * p; Set some pointers to the address of int array elements Set some pointers to the address of int array elements And set a pointer simply to an array, in C and C++ array operations are not possible, and an array name simply refers to the address of the ac tualarray object And set a pointer simply to an array, in C and C++ array operations are not possible, and an array name simply refers to the address of the ac tualarray object Print pointers –i.e. machine addresses--, also the objects pointed to, and the differences of 2 distinct pointers, both counting up and down Print pointers –i.e. machine addresses--, also the objects pointed to, and the differences of 2 distinct pointers, both counting up and down Note that “difference of 2 pointers” means, the distance between 2 machine addresses, i.e. this is very low-level machine architecture detail, one of the unique features of C and C++ Note that “difference of 2 pointers” means, the distance between 2 machine addresses, i.e. this is very low-level machine architecture detail, one of the unique features of C and C++

4 Implement int Pointers #include #include int prime[] = { 0, 1, 2, 3, 5, 7, 11, 13 }; int prime[] = { 0, 1, 2, 3, 5, 7, 11, 13 }; int *p = & prime[ 2 ]; int *p = & prime[ 2 ]; int *q = & prime[ 4 ]; int *q = & prime[ 4 ]; int *r = prime; int *r = prime; int main() int main() { // main { // main cout << "*p = prime[2] = 2 = " << *p << endl; cout << "*p = prime[2] = 2 = " << *p << endl; cout << "*q = prime[4] = 5 = " << *q << endl; cout << "*q = prime[4] = 5 = " << *q << endl; cout << "*r = prime[0] = 0 = " << *r << endl; cout << "*r = prime[0] = 0 = " << *r << endl; printf( "p = %d\n", p ); printf( "p = %d\n", p ); printf( "q = %d\n", q ); printf( "q = %d\n", q ); printf( "r = %d\n", r ); printf( "r = %d\n", r ); printf( "r+1 = %d\n", r+1 ); printf( "r+1 = %d\n", r+1 ); printf( "*(r+1) = %d\n", *(r+1) ); printf( "*(r+1) = %d\n", *(r+1) ); printf( "p-q = %d\n", p-q ); printf( "p-q = %d\n", p-q ); printf( "q-p = %d\n", q-p ); printf( "q-p = %d\n", q-p ); return 0; } // end main } // end main

5 Output for int Pointers *p = prime[2] = 2 = 2 *q = prime[4] = 5 = 5 *r = prime[0] = 0 = 0 p = q = r = r+1 = *(r+1) = 1 p-q = -2 q-p = 2

6 Output for int Pointers As expected, p points to prime[ 2 ], and q to prime[ 4 ] As expected, p points to prime[ 2 ], and q to prime[ 4 ] Also expected, actual output on a real machine, is the order of the 3 pointers values themselves: r is set to point to the whole array prime[], which is the address of prime[ 0 ], which is 8 bytes before prime[ 2 ] Also expected, actual output on a real machine, is the order of the 3 pointers values themselves: r is set to point to the whole array prime[], which is the address of prime[ 0 ], which is 8 bytes before prime[ 2 ] Interesting that the value “r+1”, which is “the next address after r” is 4 larger than that of r, i.e. 4 bytes further in the address range Interesting that the value “r+1”, which is “the next address after r” is 4 larger than that of r, i.e. 4 bytes further in the address range And the absolute value of p-q is 2, not 8. The byte offset would be 8, but the types or p and q are “pointer to int”, then the next or previous element is indeed + -1; here the delta is 2, the indices are 2 and 4 And the absolute value of p-q is 2, not 8. The byte offset would be 8, but the types or p and q are “pointer to int”, then the next or previous element is indeed + -1; here the delta is 2, the indices are 2 and 4

7 Linked char List via Pointers We build a linked list of dynamically allocated elements, each called a link We build a linked list of dynamically allocated elements, each called a link Each link holds a char named, “c” and a pointer to the next link, named “next” Each link holds a char named, “c” and a pointer to the next link, named “next” Struct ll_type is the type name for a structure with these 2 elements c and next Struct ll_type is the type name for a structure with these 2 elements c and next The type pointers to such a struct is ll_ptr_type The type pointers to such a struct is ll_ptr_type Global pointer ll is of such a type, initially nil, in C++ represented by 0, symbolically NULL Global pointer ll is of such a type, initially nil, in C++ represented by 0, symbolically NULL Read characters from the console and link in a new element, as long as the char read is not the ‘$’ Read characters from the console and link in a new element, as long as the char read is not the ‘$’ Finally, the list is pointed to by ll Finally, the list is pointed to by ll

8 Forward References Tress and graphs etc. typically use pointers to elements of types that have not yet been declared Tress and graphs etc. typically use pointers to elements of types that have not yet been declared This is, because the type of such pointers is self- referential, i.e. the type of objects being pointed to includes such pointers themselves This is, because the type of such pointers is self- referential, i.e. the type of objects being pointed to includes such pointers themselves Sounds like a “chicken and egg” problem, matter of fact it is Sounds like a “chicken and egg” problem, matter of fact it is Hence C++ has suitable struct methods to resolve Hence C++ has suitable struct methods to resolve One allows a forward reference, only for pointer types, pointer sizes are always known! One allows a forward reference, only for pointer types, pointer sizes are always known! The other method uses part of the struct type under definition The other method uses part of the struct type under definition

9 Forward Reference for Pointer // define incomplete since “forward” ref’ed ptr // sicne struct node_tp is unknown at this point // typedef struct node_tp * node_ptr_tp;// forward ref // Now OK to define objects of node_ptr_tp // Size is known to compiler: is size of pointer // node_ptr_tp root = NULL;// root of new tree // and now resolve the forward-referenced type typedef struct node_tp// being defined {// the struct charc;// track alpha intcount;// how many alphas? node_ptr_tpleft;// left subtree node_ptr_tpright;// right subtree } node_struct_tp;// new struct type

10 Forward Reference, Reuse struct // struct needs 2 pointers to self, its own type typedef struct node_tp // being defined { // the struct char c; // track alpha int count; // how many alphas? struct node_tp* left; // left subtree struct node_tp* right; // right subtree } node_struct_tp; // new struct type // one way to define pointers to this struct type: node_struct_tp* root1 = NULL; // OK // another way to define pointers to this struct type: typedef node_struct_tp * node_ptr_tp;// new ptr type node_ptr_tp root2 = NULL; // also OK // and a third way: struct node_tp * root3 = NULL; // OK, not good!

11 Implement Linked char List via Pointers // possible solution 1 #include #include typedef struct ll_tp// element of linked list of chars { char c;// actual data struct ll_tp * next;// link to next, if any } ll_type;// symbolic type name typedef ll_type * ll_ptr_type; ll_ptr_type ll = 0;// global pointer to linked list

12 Implement Linked char List via Pointers // possible solution 2 #include #include typedef struct ll_tp * ll_ptr_type;// forward ref’ed typedef struct ll_tp// being defined {// ll of chars char c; char c; ll_ptr_type next;// being used ll_ptr_type next;// being used } ll_type;// new struct type ll_ptr_type ll = NULL;

13 Build Linked char List via Pointers // allocate new node: call malloc() // change global ll void build_ll( ll_ptr_type ll_pin, char c ) { // build_ll ll_ptr_type temp = ll_ptr_type temp = (ll_ptr_type)malloc( sizeof( ll_type ) ); (ll_ptr_type)malloc( sizeof( ll_type ) ); if( temp ) { if( temp ) { temp->next = ll_pin; temp->next = ll_pin; temp->c = c; temp->c = c; ll = temp; ll = temp; }else{ }else{ printf( " no heap space in build_ll()\n" ); printf( " no heap space in build_ll()\n" ); } //end if } //end if } // end build_ll // uses global l void init() { // init char c; char c; while ( ( ( c = getchar() ) != '$' ) && while ( ( ( c = getchar() ) != '$' ) && ( c != EOF ) ) { ( c != EOF ) ) { build_ll( ll, c ); build_ll( ll, c ); } // end while } // end while } // end init

14 Linked List print() and main() // new: recursion! void print( ll_ptr_type ll ) { // print if ( ll ) { print( ll->next );// prints last element first printf( "%c ", ll->c ); } // end if } // end print int main() { // main printf( "enter string of characters end with $:" ); printf( "enter string of characters end with $:" ); init(); init(); print( ll ); print( ll ); return 0; return 0; } //end main

15 Output for Linked char List Void function print() is passed the global pointer to link elements Void function print() is passed the global pointer to link elements If not null, the current element is not yet printed, but the next ll->next is processed first! If not null, the current element is not yet printed, but the next ll->next is processed first! Once the last link element is reached, and next is NULL, the actual char in field ->c is printed Once the last link element is reached, and next is NULL, the actual char in field ->c is printed Overall the list of chars is printed in the reverse order stored, which is the original order entered in the list! Overall the list of chars is printed in the reverse order stored, which is the original order entered in the list!