Pass-by-Pointer Pointers are also used in C to enable a function to modify a variable held by the caller: void findExtrema(const int *pA, int Sz, int *pMin,

Slides:



Advertisements
Similar presentations
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.
Advertisements

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:
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!
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers Applications
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Chapter 6 Methods Chapter 6 - Methods.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Dynamic Allocation in C
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
The C++ Data Types Fundamental Data Types
Pointers and Dynamic Arrays
Pointers and Dynamic Arrays
Stack and Heap Memory Stack resident variables include:
The Machine Model Memory
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Pointers.
Introduction to Linked Lists
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Lesson One – Creating a thread
Motivation and Overview
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers and Memory Overview
Pointers and Pointer-Based Strings
C Basics.
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Pointers Psst… over there.
Pointers and References
Fundamental Data Types
Introduction to Linked Lists
void Pointers Lesson xx
Pointers and References
Pointers Psst… over there.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Pointers and Dynamic Variables
References, const and classes
Dynamic Allocation in C
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Overloading functions
C++ Pointers and Strings
Pointers Pointers point to memory locations
Fundamental Data Types
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Data Structures and Algorithms Introduction to Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C++ Pointers and Strings
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Pointers and pointer applications
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

Pass-by-Pointer Pointers are also used in C to enable a function to modify a variable held by the caller: void findExtrema(const int *pA, int Sz, int *pMin, int *pMax) { *pMin = *pMax = pA[0]; // prime the min/max values for (int idx = 1; idx < Sz; idx++) { int Current = pA[idx]; // avoid extra array // index operations if ( Current < *pMin ) *pMin = Current; else if ( Current > *pMax ) *pMax = Current; }

Pass-by-Pointer Pointers are also used in C to enable a function to modify a variable held by the caller: void findExtrema(const int *pA, int Sz, int *pMin, int *pMax) { *pMin = *pMax = pA[0]; // prime the min/max values for (int idx = 1; idx < Sz; idx++) { int Current = pA[idx]; // avoid extra array // index operations if ( Current < *pMin ) *pMin = Current; else if ( Current > *pMax ) *pMax = Current; } // calling side: int List[5] = {34, 17, 22, 89, 4}; int lMin = 0, lMax = 0; findExtrema(List, 5, &lMin, &lMax);

Returning a Pointer (Good) Pointers can also be used as return values: double* createArray(int Sz) { double *p = malloc( Sz * sizeof(double)); if ( p != NULL ) { for (int idx = 0; idx < Sz; idx++) p[idx] = 0.0; } return p; // ownership goes to caller . . . double *Array = createArray(1000);

Returning a Pointer (Bad) But… NEVER return a pointer to an automatic local object: int* F() { int Local = rand() % 1000; // Local ceases to exist when F() // executes its return, since Local has // automatic storage duration. return &Local; } . . . int *p = F(); C:\Code> gcc-4 -o P5 –std=c99 P5.c P5.c: In function 'F': P5.c:32: warning: function returns address of local variable

Pointers and const const can be applied in interesting ways in pointer contexts: int* p; // pointer and target can both be changed const int* p; // pointer can be changed; target cannot int* const p; // target can be changed; pointer cannot const int* const p; // neither pointer nor target can be changed In the latter two cases, unless you are declaring a parameter, you must initialize the pointer in its declaration. This provides safety against inadvertent changes to a pointer and/or its target, and is certainly an under-used feature in C. void findExtrema(const int* const pA, int Sz, int* const pMin, int* const pMax);

Using const with Pointers Here's an improved version of the findExtrema() function: void findExtrema(const int * const pA, // 1 int Sz, int * const pMin, // 2 int * const pMax) { . . . } 1: Now, the function cannot make pA point to anything else, nor can it change the values in the array that pA points to. 2: Now, the function cannot make pMin or pMax point to anything else, but we do need to let it change the values of the targets of pMin and pMax.

void Pointers In C, a pointer may be declared of type void: void* p; // target can be of ANY type; so no compile-time // type-checking occurs void pointers are not useful in many situations: - the return value from malloc() is actually a void* - they can be used to achieve generic programming, often with data structures, but also with a number of useful functions: void* memcpy(void* s1, const void* s2, size_t n); // The memcpy function copies n characters from the object // pointed to by s2 into the object pointed to by s1. If // copying takes place between objects that overlap, the // behavior is undefined. // Returns: the memcpy function returns the value of s1.

Pointers to Pointers A pointer can point to a pointer. One use of this is to pass a pointer so that a function can modify it: void createArray(double** const A, int Sz) { double* p = malloc( Sz * sizeof(double)); if ( p != NULL ) { for (int idx = 0; idx < Sz; idx++) p[idx] = 0.0; } *A = p; . . . double *Array; createArray(&Array, 1000);

Dereferencing a Pointer We said earlier that dereferencing a pointer yields the target of the pointer. But, there's a bit more to it than that… the C Standard says that: - if the operand p points to an object then the result of *p is a lvalue designating the object - if the operand p is of type "pointer to type" then the result of *p has type type (An lvalue is "an expression … that potentially designates an object".)

Pointer Casts Pointer typecasting can be used to define the amount of data dereferencing yields. Suppose that you run a program and give it your PID as a parameter: argv[0] --> "prog" argv[1] --> "wmcquain" CentOS > prog wmcquain Then suppose the code in main() does this: uint32_t limit = (uint32_t)(*(uint32_t*)argv[1]);

Pointer Casts . . . (uint32_t*)argv[1]); The pointer cast takes the pointer argv[1] and produces a nameless pointer of type uint32_t* . . . *(uint32_t*)argv[1]); Dereferencing that pointer yields 4 bytes of data, because the target of a uint32_t* is 4 bytes in size. . . .(uint32_t)(*(uint32_t*)argv[1]); The final typecast tells the compiler to interpret those 4 bytes as representing an unsigned integer value. "wmcq" --> 77 6D 63 71 0x71636D77 --> 1902341495

Pointer Casts Suppose the pointer p points to the beginning of a memory region: 43 17 00 A2 98 BB 0C 80 F2 DA p *(uint8_t*)p *(uint16_t*)p *(uint32_t*)p *(uint64_t*)p