C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Similar documents
C Pointers. 7.2 Pointer Variable Definitions and Initialization

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Pointers and Strings Prentice Hall, Inc. All rights reserved.

C Pointers Pearson Education, Inc. All rights reserved.

Lecture 05 POINTERS 1

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

by Pearson Education, Inc. All Rights Reserved.

Chapter 6 - Pointers

Programming for Engineers Pointers

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

Fundamentals of Programming Session 20

C++ Programming Chapter 7 Pointers

Chapter 7 C Pointers

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Fundamentals of Programming Session 19

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

MYcsvtu Notes LECTURE 34. POINTERS

Fundamentals of Programming Session 19

PROGRAMMAZIONE I A.A. 2017/2018

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

DECLARAING AND INITIALIZING POINTERS

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Fundamentals of Programming Session 20

Pointers in C. Recap: Anatomy of a Typical C Program. declarations variables functions. int main (void){ declarations; statements; return value; }

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

C++ for Java Programmers

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Pointers and Strings. Adhi Harmoko S, M.Komp

Fundamental of Programming (C)

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Variation of Pointers

Lab 3. Pointers Programming Lab (Using C) XU Silei

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

CSC 211 Intermediate Programming. Arrays & Pointers

Pointers and Strings Prentice Hall, Inc. All rights reserved.

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Lecture 9 - Pointers 1

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Pointers. Variable Declaration. Chapter 10

Work relative to other classes

CSC 211 Intermediate Programming. Pointers

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Fundamental of Programming (C)

Tutorial 10 Pointers in C. Shuyue Hu

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Language comparison. C has pointers. Java has references. C++ has pointers and references

Computer Programming Lecture 12 Pointers

First of all, it is a variable, just like other variables you studied

Assist. Prof. Dr. Caner ÖZCAN

Relationship between Pointers and Arrays

CSCI 171 Chapter Outlines

today cs3157-fall2002-sklar-lect05 1

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Pointers. 10/5/07 Pointers 1

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Lecture 4. Default Arguments. Set defaults in function prototype. int myfunction( int x = 1, int y = 2, int z = 3 );

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

POINTER & REFERENCE VARIABLES

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Problem Solving and 'C' Programming

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

What is an algorithm?

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

C: How to Program. Week /Apr/23

Fundamentals of Programming

CS102: Variables and Expressions

Content. In this chapter, you will learn:

Pointers as Arguments

Pointers in C/C++ 1 Memory Addresses 2

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Arrays, Pointers and Memory Management

Example: Pointer Basics

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Binghamton University. CS-211 Fall Pointers vs. Arrays

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Introduction to C. Systems Programming Concepts

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Lecture 04 Introduction to pointers

Transcription:

55:017, Computers in Engineering C Pointers C Pointers Powerful C feature but challenging to understand Some uses of pointers include Call by reference parameter passage Dynamic data structures Data structures that can shrink or grow Creating arrays during program execution Other examples include linked lists, stacks and trees Indirection Indirection = referencing a value through a pointer int count = 7; /* Regular int variable */ int *countptr; /* Pointer to int */ countptr = &count; /*Set countptr to point to count */ /* Can now use count and *countptr interchangeably */ count = count +1; /* is the same as */ *countptr = *countptr +1; countptr 7 count Creating Pointers Pointers (like any other variables) must be declared before they can be used Examples int *countptr, count; countptr is declared as type int * int * means a pointer to an integer value countptr is a pointer to an int countptr holds the address in memory of an integer value The * can be used to define a pointer to any C data type. Pointer Declarations General format for declaring a variable as a pointer to a particular type: name-of-type *nameofpointer This declares nameofpointer as a pointer int *iptr; /* pointer to type int */ float *fptr; char *cptr; double *dptr; What are iptr, fptr, cptr, and dptr? Each is a pointer to its associated data type Pointer Declarations Read pointer declarations from right to left and substitute the word address for the * operator int * iptr; iptr is address of an integer int * iptr; *iptr is an integer 1

Pointer Declarations Read pointer declarations from right to left and substitute the word address for the * operator int * * iptr; iptr is an address of an address of an integer *iptr is an address of an integer **iptr is an integer ***iptr is a syntax error Initializing Pointers Pointers should be initialized when declared, or in an assignment statement Initialization needed before they are used (just like other variables) Pointer initialization values NULL: Equivalent to zero, defined in stdio.h, used to indicate a pointer to nothing A memory address (usually of another variable) or from a memory allocation function) Pointer Operators Address operator, & Unary operator (needs only 1 operand) Returns the address of an operand Example int y = 5; int *yptr; /* Assigns the address of the variable y to the pointer variable yptr */ yptr = &y; yptr y Pointer vs. Addresses Pointer variables actually hold the memory address of the location that they point to. 6000 7 yptr memory address 6000 y Memory addresses are assigned by the compiler so we generally don t care about the specific address value stored in a pointer we only need to know what it points to Indirection Operator The * that we see in expressions using pointers is the indirection operator Also called the dereferencing operator Returns the value of the data to which the pointer is pointing Example: printf("%d", *yptr); /* prints 7 */ Using * like this is called dereferencing a pointer /* Using the & and * operators fig7_4.c */ main() { int a; /* a is an integer */ int *aptr; /* aptr is a pointer to an integer */ a = 7; aptr = &a; /* aptr set to address of a */ printf("the address of a is %p\n" "The value of aptr is %p\n\n", &a, aptr); printf("the value of a is %d\n" "The value of *aptr is %d\n\n", a, *aptr); printf( Showing that * and & are complements of " "each other.\n, "&*aptr = %p\n, "*&aptr = %p\n", &*aptr, *&aptr); Note: The %p format specifier allows us return 0; to print an address as a hexadecimal } number 2

Output from Preceding Program The address of a is 7ffff030 The value of aptr is 7ffff030 The value of a is 7 The value of *aptr is 7 Showing that * and & are complements of each other &*aptr = 7ffff030 *&aptr = 7ffff030 Another Pointer Example double flt1 = 100.0; double *flt_ptr; flt_ptr = &flt1; Note: flt_ptr does not store a double value but rather a pointer to another variable (flt1) that contains a double Pointer is useless unless it s pointing to something Indirection with the * operator retrieves the value to which the pointer refers int *iptr, n=7, j=23, i=3; iptr = &i; n = *iptr; *iptr = j; *iptr = *iptr + n + 10; /* Trace: What is the value of *iptr? */ Solution: Trace all variable at each line of code int *iptr, n=7, j=23, i=3; iptr = &i; *iptr=i=3, n=7, j=23 n = *iptr; *iptr=i=3, n=3, j=23 *iptr = j; *iptr=i=23, n=3, j=23 *iptr = *iptr + n + 10; *iptr=i=23+3+10=36, n=3, j=23 What is going on in the computer memory? 1. int *iptr, n=7, j=23, i=3; What is going on in the computer memory? 1. int *iptr, n=7, j=23, i=3; 2. iptr = &i; 3. n = *iptr; 4. *iptr = j; 5. *iptr = *iptr + n + 10; Result of line 1. Result of line 2. 2. iptr = &i; 3. n = *iptr; 4. *iptr = j; 5. *iptr = *iptr + n + 10; Result of line 3. Result of line 4. iptr n j i 6000 unknown 6004 7 6008 23 6012 3 iptr n j *iptr, i 6000 6012 6004 7 6008 23 6012 3 iptr 6000 6012 n 6004 3 j 6008 23 *iptr, i 6012 3 iptr n j *iptr, i 6000 6012 6004 3 6008 23 6012 23 3

What is going on in the computer memory? 1. int *iptr, n=7, j=23, i=3; 2. iptr = &i; 3. n = *iptr; 4. *iptr = j; 5. *iptr = *iptr + n + 10; Result of line 4. iptr 6000 6012 n 6004 3 j 6008 23 *iptr, i 6012 23 *iptr = 23 + 3 + 10 = 36 Result of line 5. iptr 6000 6012 n 6004 3 j 6008 23 *iptr, i 6012 36 Pointer Expressions Pointers can be used in arithmetic expressions assignment expressions comparison expressions Not all operators are valid for use with pointer variables Before doing arithmetic on pointer variables, be sure that you know what you re doing Pointer Operators Pointers can be: incremented (++) decremented (- -) increased via integer addition ( + or +=) decreased via integer subtraction (- or -=) one pointer may be subtracted from another Pointer Arithmetic Assume integers take up 4 memory locations (bytes) v[0] v[1] v[2] v[3] v[4] v[5] 2048 2052 2056 2060 2064 2068 vp vp+1 vp+2 vp+3 vp+4 vp+5 int v[6], *vp; vp = v; /* vp =?? */ vp += 2; /* vp =?? */ vp = &v[3]; /* vp =?? */ Pointer Arithmetic Answer In conventional arithmetic 2048 + 2 = 2050 But in pointer arithmetic, 2048 + 2 * 4 = 2056! where 4 is the number of memory locations for an int data type A double takes 8 memory locations and double *dptr, d[10]; dptr = d; /* assume array starts at address 3000 */ dptr += 2; /* dptr =3000 + 2 * 8 = 3016 */ More Pointer Arithmetic Using increment to point to next location in array vptr++; vptr--; Subtracting pointers: int v[6], x; int *vptr1, *vptr2; vptr1=v; /* same as: vptr1 = &v[0] */ vptr2 = &v[3]; /* same as vptr2 = vptr1 + 3 */ x = vptr2 vptr1; printf( x = %d\n, x); Prints: x = 3 4

Pointer Comparison Pointers can be compared using Equality (i.e. ==) Relational operators (e.g. >) Meaningless unless pointers point to members of the same array or some other well-understood data structure Most common use of pointer comparison is determining whether a pointer is NULL if (vptr!= NULL) A pointer can be assigned to another pointer only if they are of the same data type. Otherwise, a cast operator must be used Convert the pointer on the right of the assignment to the pointer of the type on the left float *floatptr; double *doubleptr; doubleptr = (double *) floatptr; This can get you into a lot of trouble. Before casting pointers, be absolutely sure that you know what you are doing. The pitfalls of casting pointers float *floatptr; double *doubleptr; float f = 23.5678; double d = 23.5678; floatptr = &f; doubleptr = &d; printf("the value of *floatptr is %f\n", *floatptr); printf("the value of *doubleptr is %f\n", *doubleptr); doubleptr = (double *) floatptr; printf("now, the value of *doubleptr is %f\n", *doubleptr); return 0; } Prints: The value of *floatptr is 23.567801 The value of *doubleptr is 23.567800 Now, the value of *doubleptr is 478862080.000000 Why??? Pointers and Arrays Pointer and array variables both refer to addresses in memory Arrays and pointers are (almost) always interchangeable An (unsubscripted )array name is actually a a constant pointer points at first element of the array Cannot assign a new address to an array variable Pointers can be used to do any operation involving subscripting, just like arrays Subscripting vs. pointer notation for array access There are four different ways to access elements of an array Array subscript notation Pointer + offset notation using array name Pointer + offset notation Pointer subscript notation Array Subscript Notation Array subscript notation means the form int n[10]; n[1] = 5; int j, offset, c[ ] = {20,30,40, 50}; int *cptr; cptr = c; printf("c[1] = %d\n", c[1]); /* What does this print? */ 5

Pointer Offset Notation with Array Name Pointer offset notation means the form: int n[10]; *(n + 1) = 5; int j, offset, c[ ] = {20,30,40,50};,, int *cptr; cptr = c; offset = 2; printf("*(c + %d) = %d\n", offset, *(c + offset)); /* What does this print? */ Pointer Offset Notation Pointer offset notation means the form int n[10], *nptr; nptr = n; *(nptr + 1) = 5; int j, offset, c[ ] = {20,30,40, 50}; int *cptr; cptr = &c[0]; /*note: different init*/ offset = 3; printf("*(cptr + %d) = %d\n", offset, *(cptr + offset)); /* What does this print? */ Pointer Subscript Notation Pointer subscript notation means the form int n[10], nptr; nptr = n; nptr[1] = 5; int j, offset, c[ ] = {20,30,40,50}; int *cptr = c; offset = 1; printf("cptr[%d] = %d\n", offset, cptr[offset])); /* What does this print? */ Notational Equivalences int b[5]; int *bptr; bptr = b; is same as bptr = &b[0]; b[3] same as *(bptr + 3) same as *(b+3) same as bptr[3] Not the same: *(bptr + 3) not same as *bptr + 3 The same: &b[3] same as bptr + 3 Generic (void) pointer void * pointer Pointer to void (i.e. void *) Generic pointer that can represent any pointer type All pointer types can be assigned a pointer to void, A generic pointer can be assigned to any type A generic pointer can not be dereferenced Simply contains a memory location for an unknown data type (i.e. # of bytes unknown by compiler) Also cannot do pointer arithmetic on generic pointers These wlll be useful later for dynamic memory allocation Using the const qualifier with pointers The const qualifier tells the compiler that the value of a particular variable should not be modified. Six possibilities exist for using (or not using) const with function parameters two with call-by-value parameter passing four with call-by-reference parameter passing Use principle of least privilege to choose which one to use. Give a function enough access to the data in its parameters to accomplish its specified task, but no more. 6

Using the const qualifier Different ways to use the const qualifier to pass variables to a function using call-by-value funtion1(int t x) read right to left: x is an integer can change value of x in function function2(const int x) read right to left: x is a constant integer can t change value of x in function Using the const qualifier Different ways to use the const qualifier to pass variables to a function, call-by-reference function3(int * xptr) read right to left: xptr is an address of an integer, *xptr is an integer can change address stored in xptr and can change the data stored at that address function4(int * const xptr) read right to left: xptr is a constant address to an integer, *xptr is an integer can t change address stored in xptr but can change the data stored at that address function5(const int * xptr) read right to left: xptr is an address to a constant integer, *xptr is a const int can change address stored in xptr but can t change the data stored at that address function6(const int * const xptr) read right to left xptr is a constant address to a constant int, *xptr is a const int can t change address stored in xptr and can t change the data stored at that address Principle of Least Privilege If a variable does not (or should not) change in the body of a function to which it is passed, the variable should be declared const to ensure that it s not accidentally modified. Example: Consider a function that takes a singlesubscripted array and its size arguments and prints the array. Neither the size of the array nor its contents should change in the function body. Four ways to pass a pointer as an argument to a function non-constant pointer to non-constant data (highest level of access) constant pointer to non-constant data non-constant t pointer to constant t data constant pointer to constant data (lowest level of access) /* Modified from Fig. 7.10: fig07_10.c Converting lowercase letters to uppercase letters using a non-constant pointer to non-constant data*/ #include <ctype.h> #define STRSIZE 80; void converttouppercase( char *sptr ); /* prototype */ char * string; string = (char *)malloc(sizeof(char)*strsize); strcpy(string, "characters and $32.98"); printf( "The string before conversion is: %s", string ); converttouppercase( string ); printf( "\nthe string after conversion is: %s\n", string ); /* convert string to uppercase letters */ void converttouppercase( char *sptr ) { while ( *sptr!= \0 ) { if ( islower( *sptr ) ) { /* if lowercase, */ *sptr = toupper( *sptr ); /* convert to */ } /* uppercase */ ++sptr; /* move sptr to the next character */ } /* end while */ } /* end function converttouppercase */ Program Output: The string before conversion is: characters and $32.98 The string after conversion is: CHARACTERS AND $32.98 7

/* Fig. 7.11: fig07_11.c Printing a string one character at a time using a non-constant pointer to constant data */ void printcharacters( const char *sptr ); /* initialize char array */ char string[] = "print characters of a string"; printf( "The string is:\n" ); printcharacters( string ); printf( "\n" ); /* sptr cannot modify the character to which it points, i.e., sptr is a "read-only" pointer */ void printcharacters( const char *sptr ) { /* loop through entire string */ for ( ; *sptr!= \0 ; sptr++ ){/*no initialization*/ printf( "%c", *sptr ); } /* end for */ } /* end function printcharacters */ Program Output: The string is: print characters of a string /* Fig. 7.12: fig07_12.c Attempting to modify data through a non-constant pointer to constant data. */ void f( const int *xptr ); /* prototype */ int y; /* define y */ f( &y ); /* f attempts illegal modification */ /* xptr cannot be used to modify the value of the variable to which it points */ void f( const int *xptr ) { *xptr = 100; /*error: cannot modify a const object*/ } /* end function f */ /* Fig. 7.13: fig07_13.c Attempting to modify a constant pointer to non-constant data */ int x; /* define x */ int y; /* define y */ /* ptr is a constant pointer to an integer that can be modified through ptr, but ptr always points to the same memory location */ int * const ptr = &x; *ptr = 7; /* allowed: *ptr is not const */ ptr = &y; /* error: ptr is const; cannot assign new address */ /* Fig. 7.14: fig07_14.c Attempting to modify a constant pointer to constant data. */ int x = 5; /* initialize x */ int y; /* define y */ /* t i t t i t t t t i t /* ptr is a constant pointer to a constant integer. ptr always points to the same location; the integer at that location cannot be modified */ const int *const ptr = &x; printf( "%d\n", *ptr ); *ptr = 7; /* error: *ptr is const; cannot assign new value */ ptr = &y; /* error: ptr is const; cannot assign new address */ 8