ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
Computer Science 210 Computer Organization Pointers.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
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++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CS1010 Programming Methodology
CS1010 Programming Methodology
Chapter 7 Pointers and C-Strings
Computer Science 210 Computer Organization
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Lecture 6 C++ Programming
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Basic notes on pointers in C
Computer Science 210 Computer Organization
Pointers Psst… over there.
Pointers.
PZ09A - Activation records
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
ECE 103 Engineering Programming Chapter 32 Array Parameters
ECE 103 Engineering Programming Chapter 56 Runtime Errors
Pointer Operations.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 62 Stack Implementation
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 63 Queue Implementation
C Programming Lecture-8 Pointers and Memory Management
Herbert G. Mayer, PSU CS Status 7/19/2015
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CS148 Introduction to Programming II
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1 Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus Non-pointer Variables Pointer Variables Levels of Indirection

Non-pointer Variables A standard (non-pointer) variable stores values Declaration: datatype variable_name; Example: int x; x contains a value that is of type datatype The value stored in x is accessed directly & x is memory address where value of x is stored 2

What memory operations are performed? Example: int x, y; x = 3; y = x; What memory operations are performed? 3

int x, y; x = 3; y = x; x y x x y x y x y 3 3 3 3 Address Ax Address Ay copy 3 x Address Ax x Address Ax y Address Ay 3 copy x Address Ax y Address Ay 3 x Address Ax y Address Ay 3 4

Pointer Variables A pointer is a variable that can contain only the address of something else That something else may be another variable, function, or data object How are pointers used in C? To allow a function to permanently change arguments To access data or functions indirectly To track dynamically allocated memory at runtime To build advanced data structures such as linked lists 5

Declaration: datatype * p_name; The * symbol declares a variable as a pointer p_name contains an address p_name is said to “reference” or to “point to” the memory location at that address datatype is the type of value stored at the address The * symbol is also used to “dereference” a pointer: If p contains an address, then *p retrieves the value stored at that address (aka indirect access) & p_name is the address in memory where the pointer variable p_name itself is stored 6

Example: g is a pointer of type float * and references address 500. The memory at address 500 holds value 1.25., and g itself is stored at address 384: g contains the value 500 * g returns the value 1.25 & g returns the value 384 g 384 500 500 1.25 7

A pointer variable should be initialized to a valid address before being used: Example: float var = 3, count; /* Storage */ float *p1, *p2 = & var; /* Pointers */ p1 = &count; printf( "%p %p %f\n", &p2, p2, *p2 ); NULL is a predefined macro used for initializing a pointer so that it does not point to anything Example: double *p, *q = NULL; p = NULL; 8

Examples: int x, y, z; int *p, *q, *r; /* Pointers */ x = 3; p = & x; q = & y; r = p; y = x; z = *p; *p = 7; *q = *p; 9

int *p, *q, *r; /* Pointers */ int x, y, z; int *p, *q, *r; /* Pointers */ x = 3; (store a 3 in x) x Address Ax y Address Ay p Address Ap q Address Aq z Address Az r Address Ar 3 x Address Ax x Address Ax 3 10

p = &x; (store address of x in pointer p) q = &y; (store address of y in pointer q) Ax p Address Ap x Address Ax 3 p Address Ap x Address Ax Ax 3 Ay q Address Aq y Address Ay q Address Aq y Address Ay Ay 11

r = p; (copy contents of pointer p to pointer r) Address Ap r Address Ar Ax r Address Ar Ax p Address Ap x Address Ax 3 12

y = x; (copy contents of x to y) z = *p; (copy contents of x to z via *p) x Address Ax y Address Ay 3 y Address Ay 3 p Address Ap x Address Ax Ax z Address Az 3 z Address Az 3 p contains a memory address. If *p is on the right side of the equal sign, it retrieves the value stored at that memory address. 13

*p = 7; (store a 7 in x via *p) Address Ap x Address Ax Ax 7 3 x Address Ax 7 p contains a memory address. If *p is on the left side of the equal sign, the memory location that p points to will store the value on the right side. 14

*q = *p; (copy contents of x to y via pointers) Address Aq y Address Ay Ay p Address Ap x Address Ax Ax 7 3 y Address Ay 7 p contains a memory address. q contains a memory address. The memory location that q points to will get the value that is pointed to by p. 15

Assume addresses: x→100 y→104 z→108 p→120 q→124 r→128 Assume type int is 32 bit, and that pointers are also 32 bit. Start   End x = 3; p = &x; q = &y; r = p; y = x; z = *p; *p = 7; *q = *p; Addr Var Value 100 x 3 7 104 y ? 108 z 120 p 124 q 128 r 16

Levels of Indirection C supports multiple levels of indirection; e.g. Declarations: One level - datatype *pname; Pointer (e.g., int *p;) Two level - datatype **pname; Pointer to a pointer (e.g., float **q;) Three level - datatype ***pname; Pointer to a pointer to a pointer (e.g., double ***w;) 17

What does “multiple levels of indirection” mean? Example: int x; int *p; int **q; Standard variable → It stores a value of type integer. Pointer variable → It holds the address of a location in memory where data is stored. Pointer to a pointer variable → It holds the address of a location in memory that holds the address of another memory location where data is stored. 18

Example: int x; int *p; /* Pointer */ int **q; /* Pointer to another pointer */ x = 5; p = &x; /* p points to x */ q = &p; /* q points to p which points to x */ printf("%d %d %d\n", x, *p, **q); /* Displays 5 5 5 */ *p = 4; /* One level of indirect access */ printf("%d %d %d\n", x, *p, **q); /* Displays 4 4 4 */ **q = 3; /* Two levels of indirect access */ printf("%d %d %d\n", x, *p, **q); /* Displays 3 3 3 */ p = NULL; 19

Assume addresses: x→1150 p→1300 q→1500 Assume type int is 32 bit, and that pointers are also 32 bit. Start   End x = 5; p = &x; q = &p; *p = 4; **q = 3; p = NULL; Addr Var Value 1150 x 5 4 3 1300 p ? NULL 1500 q Conceptual diagram (just before executing *p = 4;) 1500 1300 q 1300 1150 p 1150 5 x 20

With great power comes great responsibility … Common error → Using a non-initialized pointer Pointer variable p contains random address x = *p; retrieves value from that random address → Program now mistakenly uses incorrect value *p = x; writes a value to that random address → Corrupted memory (may get memory fault message, or no warning at all) Always initialize a pointer to a valid address or NULL. If p is set to NULL, then *p generates a run-time error, which alerts you that a problem exists. 21