Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Pointers Applications
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Chapter 12 Pointers and Dynamic Arrays. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Pointers (12.1) Dynamic Arrays.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Pointers and Dynamic Arrays
Chapter 8 Arrays, Strings and Pointers
CSE 220 – C Programming Pointers.
CMPE Data Structures and Algorithms in C++ September 14 Class Meeting
COMP 2710 Software Construction Pointers
Values – also known as scalars (eg. int, double, char, float)
Lecture 6 C++ Programming
Pointers and Dynamic Variables
Programming in C Pointer Basics.
Chapter 12 Pointers and Memory Management
Pointers and Dynamic Variables
Outline Defining and using Pointers Operations on pointers
Programming in C Pointer Basics.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
7. Pointers, Dynamic Memory
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C Programming Lecture-8 Pointers and Memory Management
POINTER CONCEPT 4/15/2019.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers and dynamic objects
POINTER CONCEPT 8/3/2019.
Presentation transcript:

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer Science & Information System

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 2 Today’s Topics Concept of Variables Concept of Pointers Declaring Pointer Variables Manipulating Pointers

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 3 Variables Variable is a space in memory that is used to hold a value. Each variable has a name, content and address Variable name is a logical reference, used by the programmer Address is a physical reference or actual location, and is used by the computer

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 4 Variables A char variable uses one byte Print the addresses Output:

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 5 Variables An integer variable uses four bytes. The address of an integer variable is the first byte.

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 6 Pointer Variables What we have used so far are ordinary variables or also called data variables. A data variable contains a value (e.g. integer number, a real number or a character). A pointer variable is another type of variable that contains the address of other variable. It also known as address variable.

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 7 Pointers Example: a is a data variable p is a pointer variable that stores the address of a – that is &a.

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 8 Pointer Variable Declaration A pointer variable is declared like the data variable. The difference is, put an asterisk (*) in front of it. Example: int a=-123; // a is a data variable int *p; // p is pointer variable p = &a; // p then holds the address of variable a

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 9 Example: p and q point to the same variable. int a=-123; int *p; int *q; p=&a; q=p;

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 10 The type of a pointer must be matched with the type of the variable that the pointer points to. int n; int *p; double *q; p=&n; // this is OK q=&n; // this is wrong, type mismatch q=p; // this is also wrong

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 11 Multiple Declarations To declare multiple pointers in a statement, use the asterisk for each pointer variable Example: int *p1, p2; Only p1 is a pointer variable; p2 is an ordinary variable You may also use typedef to make the declaration clear Example: typedef int *IntPtr; IntPtr p1, p2; Now, both p1 and p2 are pointer variables

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 12 Initialization of Pointer Variables Pointer variables can also be declared and initialized in a single statement

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 13 Accessing Variables Through Pointer Variables There are two special operators for pointers: address-of operator (&) and indirection operator (*). Address-of operator, & –is used for getting the address of a variable –Example: &n meaning: “give me the address of variable n” Indirection operator (also called dereferencing operator), * –is used for getting the content of a variable whose address is stored in the pointer. –Example: *ptr meaning: “give me the content of a variable whose address is in pointer ptr” –Notice that, a pointer declaration also uses an asterisk (*). Don’t get confused. Pointer declaration and indirection operator are different. –Indirection operator must only be used with a pointer variable. If n is a data variable, the following would be an error. *n

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 14 The & and * Operators void main(void) { int a = 5; int *ptr = &a; printf("%d ",ptr); printf("%d ",&a); printf("%d ",&ptr); printf("%d ",*ptr); *ptr = *ptr + 5; printf("%d ",*ptr); printf("%d ",a); } Memory Address Content a ptr Prints 2000 The addresses are specified by the computer. The addresses above are used only for examples. Prints 2000 Prints 2004 Prints 5. This is how it works. ptr contains Go to the address 2000 and get its content => 5. Means that, the value of *ptr is 5. This means, a = a + 5, because ptr holds the address of a. The new value of a is 10 Prints printf("%d ", *a ); // This would be an error, because // a is not a pointer variable

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 15 The & and * operators are canceling each other Example: int x; int *p=&x; *&x is equivalent to x and p. However, the following would be an error because x is not a pointer. The * operator can only be used with a pointer variable. &*x // Error! This means &(*x)

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 16 int x; int *p=&x; int *q=&x;

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 17 Caution! Pointer assignments

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 18 The malloc Function Using pointers, variables can be manipulated even if there is no identifier for them. This variable is called a dynamic variable (or sometimes nameless variable) –To create a pointer to a new dynamic variable of type int, use the malloc function Example: int *p; p = (int*) malloc ( sizeof(int) ); –You have to include stdlib.h to use the malloc function p ? ? dynamic variable pointer variable

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 19 The malloc Function –The new variable can only be accessed through p. The new variable is then referred to as *p1 –It can be used anyplace an integer variable can scanf(“%d”,&*p1 ); // or simply scanf(“%d”, p1 ); *p1 = *p1 + 7; p ? ? dynamic variable pointer variable

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 20 Dynamic variables are variables that are created and destroyed while the program is running. Static variables (sometimes called automatic variables) are variables that are automatically created and destroyed by the computer. Dynamic and Static Variables p 10 dynamic variable static variable 99 static variable p n Example: int n=99; int *p; p = new int; *p = 10;

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 21 Basic Memory Management An area of memory called the freestore is reserved for dynamic variables –New dynamic variables use memory in the freestore –If all of the freestore is used, calls to new will fail Unneeded memory can be recycled –When variables are no longer needed, they can be deleted and the memory they used is returned to the freestore

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 22 The free Function When dynamic variables are no longer needed, free them to return memory to the freestore Example: The value of p is now undefined and the memory used by the variable that p pointed to is back in the freestore free(p); ? ? p After: Before: p ? ?

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 23 Pointers to Pointers Output: 58