CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.

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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
CS Winter 2011 Introduction to the C programming language.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
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.
C Slides and captured lecture (video and sound) are available at:
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
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.
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.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS 261 – Data Structures Introduction to C Programming.
CS 31 Discussion, Week 10 Faisal Alquaddoomi, Office Hours: BH 2432, F 12:30-1:30pm.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Review 1 List Data Structure List operations List Implementation Array Linked List.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
System Programming Practical Session 7 C++ Memory Handling.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Variables and memory addresses
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
UNIT 8 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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Stack and Heap Memory Stack resident variables include:
UNIT 5 C Pointers.
Motivation and Overview
Introduction to the C programming language
C Basics.
Lecture 6 C++ Programming
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Pointers.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
C Programming Lecture-8 Pointers and Memory Management
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

CS 261 – Data Structures C Pointers Review

C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a = a + 2; } … void main (int argc, char **argv) { { int b = 6; foo(b) printf(“b = %d\n”, b); } parameter argument Question: What is the output? Answer: >> b = 6 What if we want to change b?

Simulation of Pass-By-Reference C is Pass-by-value: a copy of the arguments are passed in to a parameter Changes made inside are not reflected outside What if we want to change a parameter? We simulate what is often called “Pass-By-Reference” To do so, we need to learn about Pointers

Pointers A pointer is simply a value that can refer to another location in memory In other words, its value is an address in memory! Declaring a Pointer (*) int *pVal; Initializing a Pointer pVal = 0; /* 0 means uninitialized */ Get address of (or pointer to) a stored value (&) int a = 5; pVal = &a; Dereferencing a Pointer (*) *pVal = 4;/* Assignment*/ int b = *pVal;/* Access */ 3

Pointer Example double *ptr; double pi, e; ptr = π *ptr = ; ptr = &e; *ptr = ; printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e); AddrValueName 23??.??pi24…333?ptr334335…515??.??e516

Pointer Example double *ptr; double pi, e; ptr = π *ptr = ; ptr = &e; *ptr = ; printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e); AddrValueName 23??.??pi 24 … 33323ptr … 515??.??e 516

Pointer Example double *ptr; double pi, e; ptr = π *ptr = ; ptr = &e; *ptr = ; printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e); AddrValueName pi 24 … 33323ptr … 515??.??e 516

Pointer Example double *ptr; double pi, e; ptr = π *ptr = ; ptr = &e; *ptr = ; printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e); AddrValueName pi 24 … ptr … 515??.??e 516

Pointer Example double *ptr; double pi, e; ptr = π *ptr = ; ptr = &e; *ptr = ; printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e); AddrValueName pi 24 … ptr … e 516

Pointer Example double *ptr; double pi, e; ptr = π *ptr = ; ptr = &e; *ptr = ; printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e); AddrValueName pi 24 … ptr … e 516 >> Values: 0x

Pass-By-Reference Simulation Main Idea: If I can pass an address (ie. a pointer), I can’t modify it, however, I can modify what it points to (or references)! void foo (int *a) { *a = *a + 2; } … void main (int argc, char **argv) { { int b = 6; foo(&b) printf(“b = %d\n”, b); } 10 Question: What is the output? Answer: >> b = 8

Pointers and Structures Pointers often point to structures. Introduces some new syntax: void setGateType(struct Gate *g, int gateVal) { (*g).type = gateVal; } struct Gate { int type; struct Gate *left; struct Gate *rght; };

Pointers and Structures Pointers often point to structures. Introduces some new syntax: void setGateType(struct Gate *g, int gateVal) { g->type = gateVal /* equiv to (*g).type */ } struct Gate { int type; struct Gate *left; struct Gate *rght; };

Structures and Pass-by-Reference Parameters Very common idiom: struct Vector vec; /* Note: value, not pointer. */ vectorInit(&vec); /* Pass by reference. */ vectorAdd (&vec, );

Dynamic Memory Allocation If I know exactly what I need at compile time, I can use static allocation. e.g. If I need a single struct gate or 5 struct gates 14 struct Gate p; or struct Gate p[5]; or struct Gate p1; struct Gate p2; …

Dynamic Memory Allocation But, what if I don’t know at compile time? e.g. I need N gates?...where N will be provided as a command line argument or where the user would request one at a time ? 15 /* N gates at once */ struct Gate *p = malloc(N * sizeof(struct Gate)); /* One gate at a time */ struct Gate *p = malloc(sizeof(struct Gate));

Dynamic Memory Allocation struct Gate *p; p->type = 4; /* segmentation fault error */ /* static allocation */ struct Gate p; initGate(&p); /*Fills the gate fields */

Dynamic Memory Allocation No new operator Use malloc(num-of-bytes) instead malloc always returns a pointer Use sizeof to figure out how big (how many bytes) something is struct Gate *p = malloc(sizeof(struct Gate)); assert(p != 0); /* Always a good idea. */ p->type = 3; /* safe!*/ … free(p);

Check Conditions: assert We will use assert to check all sorts of conditions Halts program if condition not found #include /* Assert checks if specified condition is true. */ assert(whatever-condition);

Arrays Arrays in C are (more or less) pointers void foo(double d[]) { /* Same as foo(double *d). */ d[0] = ; } … double data[4]; /*static*/ ordouble data* = malloc(4*sizeof(double)); /*dyn*/ data[0] = 42.0; foo(data); /* Note: NO ampersand. */ printf("What is data[0]? %g", data[0]);

Arrays int a[10] int *pa; a is a pointer to the first element of the array a[i] refers to the i-th element of the array pa=&a[0] makes pa point to element 0 of the array, in other words, pa = a a[2] is the same as *(pa+2) one difference: a pointer is a variable, but an array name is not pa = a; //legal pa++; //legal a = pa; //not legal a++;//not legal

Side Note: Booleans C versions (pre C99) did not have a boolean data type Can use ordinary integer: test is zero (false) or not zero (true) Can also use pointers: test is null/zero (false) or not null (true) int i; if (i != 0)... if (i)... /* Same thing. */ double *p; if (p != 0)... if (p)... /* Same thing. */ In C99, we can use bool, but must include header

Side Note: Unitialized Pointers What if I don’t init a pointer, and then access it? 22 struct Gate *p; p->type = 4; /* segmentation fault error */