pointer-to-pointer (double pointer)

Slides:



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

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Introduction to C Programming CE
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
1 Workin’ with Pointas An exercise in destroying your computer.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions & Pointers in C Jordan Erenrich
1 2/2/05CS250 Introduction to Computer Science II Pointers.
C Programming Chapters 11, . . .
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Generic Programming in C
Introduction to Programming Using C
CSCI206 - Computer Organization & Programming
Pointers and Pass By Reference
C/C++: type sizes in memory pointers
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Pointers and References
14th September IIT Kanpur
Dynamic Memory Allocation Reference Variables
CSC 253 Lecture 8.
Scope, Parameter Passing, Storage Specifiers
CSC 253 Lecture 8.
Lecture 18: The Elegant, Abstract World of Computing
Pointers  Week 10.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Systems Programming Concepts
Variables and Their scope
Return by Reference CSCE 121 J. Michael Moore.
Pointers & Functions.
Example passing the address of x x pint or
Functions, Part 2 of 3 Topics Functions That Return a Value
Pointers Call-by-Reference CSCI 230
prepared by Senem Kumova Metin modified by İlker Korkmaz
Introduction to Problem Solving and Programming
Dynamic Memory A whole heap of fun….
Simulating Reference Parameters in C
CSCE 206 Lab Structured Programming in C
7. Pointers, Dynamic Memory
C++ Pointers and Strings
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
Pointers & Functions.
The Stack.
Dynamic Memory Allocation
CSCE 206 Lab Structured Programming in C
Pointers and References
C Pointers Systems Programming.
Pointer Arithmetic By Anand George.
C++ Pointers and Strings
Functions, Part 2 of 3 Topics Functions That Return a Value
Pointers, Dynamic Data, and Reference Types
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to Pointers
C Parameter Passing.
Presentation transcript:

pointer-to-pointer (double pointer) "pass by pointer“ of function parameter means passing a pointer by value In most cases, no problem! Problem comes when modifying the pointer inside the function In this case, we need pointer-to-pointer. int g_One=1; void func(int* pInt); int main() { int nvar=2; int* pvar=&nvar; func(pvar); printf("%d",*pvar); // output??? return 0; } void func(int* pInt) pInt=&g_One;

Pointer-to-Pointer parameter void func(int** ppInt); int g_One=5; int main() { int nvar=2; int* pvar=&nvar; func(&pvar); // this is necessary to modify the pointer pvar in the function printf("%d",*pvar); // output??? return 0; } void func(int** ppInt) // ppInt takes the address of pointer variable //Modify the pointer, ppInt points to *ppInt=&g_One; //You can also allocate memory, depending on your requirements //*ppInt=(int*)malloc(sizeof(int)); //**ppInt=10; //Modify the variable, *ppInt points to //**ppInt=3;

example Is this OK??? #include <stdio.h> #include <stdlib.h> int main() { int* p, i; func(p); for (i=0;i<5;i++) scanf(“%d”,&p[i]); for (i=0;i<5;i++) printf(“p[%d]=%d\n”,i,p[i]); return 0; } void func(int* ptr) ptr=(int*)malloc(5*sizeof(int));