Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Programming and Data Structure
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.
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:
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
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!
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
Pointers Applications
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
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.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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:
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Variables and memory addresses
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Student Book An Introduction
Pointer.
Object Oriented Programming COP3330 / CGS5409
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Overloading functions
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Pointers and pointer applications
Pointers.
Presentation transcript:

Introduction to Pointers.

What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored in system. Pointer is always a whole number, no matter whether it holds the addr of int, char, float etc Int a=100;int *ptr; ptr=&a; Now ptr may be pointing to addr In the next run of the pgm ‘a’ may be stored in addr 6000 and ptr points to Pointer will also have an address in memory.

What is a variable? Variable in a program is something with a name, the value of which can vary. Ex: int k. When compiler sees this, it reserves space in memory(4 bytes) to hold an integer value and associate name k with the memory location reserved. This memory reserved is called the address. Later in the program if it encounters k=2, value 2 will be placed in that memory location. Pointer to a variable is nothing but pointer through which we can access this variable in memory. Hence through pointers we can change the value of a variable in the memory itself. Address of a variable is obtained by prefixing ‘&’ in front of variable.ex: &k

Address in memory Total memory depends on the size of RAM. If RAM is 64K(64*1024), then we have bytes(0 to 65535). whenever a variable is declared, compiler allocates memory at appropriate place where the data can be stored. For ex: int m=20; Compiler allocates 4 bytes of memory starting from say Hence the bytes reserved for m are 2000,2001,2002 and Hence a variable is associated with the starting address of allocated memory. Programmers do not know the address of the variable.

Why are pointers used? to perform dynamic memory allocation. To implement linked lists, trees and some other data structures. How is pointer declared? int a=10; int *ptr;//or int * ptr, int* ptr; ptr = &a; ptr a print(ptr)  2000 Print(&a)  2000 Print(*ptr) 

Hence ‘*’ before a declaration of a variable says it is a pointer variable.(int *ptr;) ‘*’ is not attached to data type during declaration. Hence int* ptr1,ptr2,ptr3. Here, only ptr1 is pointer variable, not ptr2 and ptr3. ‘&’ prefixed to a variable gives its address in memory. ‘*’ prefixed to a pointer variable gives the value present in the memory location pointed to by pointer.(*ptr) ‘&’ is called address operator or indirection operator. Address is printed using %u since address is always an unsigned integer.

Data type of a pointer variable indicates the type of the variable,the pointer is pointing to and not the type of pointer.(because pointers are always unsigned integers) Ex:int *ptr1; //ptr1 is pointer to int char *ptr2; //ptr2 is pointer to char float *ptr3; //ptr3 is pointer to float Here ptr1,ptr2,ptr3 are all unsigned integers. Any number of pointers can point to a single variable. A pointer can point to different items at different statements.

Double pointer or pointer to pointer int i=3; int *ptr1,**ptr2; ptr1=&i; ptr2=&ptr1; ptr2 ptr1 i ** indicates the variable is a pointer to pointer. Pointer to pointer holds the address of another pointer. NOTE: there is no limit to the pointer to pointers.But maximum limit that is being used is

Print(i);//3 Print(*ptr1);//3 Print(**ptr2);//3 Print(ptr2);//4500 Print(ptr1);//4000 Print(*ptr2);//4000 Ptr2 prints the value contained in its address.i.e 4500 *ptr2 prints the value contained in the address 4500.i.e 4000 **ptr2 prints the value contained in address 4000.i.e 3

How a variable is stored in memory? (Assume integer is 2 bytes) int k=5; This is actually stored as int *ptr; ptr=&k; (*ptr)  5. when (*ptr) is used to get the value of k,entire 2 bytes(2000 and 2001) is considered and hence it gives k 2000

Arithmetic operations allowed on pointers 1.Incrementing pointer 2.Decrementing 3.Adding or subtracting a number from the pointer. 4.Subtracting a pointer from a pointer(in case of array). Increment pointers are incremented using ‘++’ operator. When incremented the pointer points to the next valid byte. Increment depends on the type of data, the pointer is pointing to. Ex: incrementing an integer pointer increments the pointer by 2 bytes,char pointer by 1 byte and float by 4 bytes.

Decrementing a pointer Pointers are decremented using ‘- -’ operator. After decrement pointer points to the previous valid byte based on the type of pointer. Ex:if ptr is an integer pointer and if it points to address 2500.After ptr - -, ptr points to if ptr is a floatpointer and if it points to address 2500.After ptr - -, ptr points to 2496.

Adding or subtracting a number to and from the pointer ptr=ptr+2; //if ptr==2000. after ptr+2, ptr==2004 ptr=ptr+4 //ptr==2012 ptr=ptr-3 //ptr==2006 (here ptr is int pointer) Arithmetic operations not allowed on pointers 1.Addition of 2 pointers. 2.Multiplying or dividing a number to a pointer. ptr=ptr*2; //invalid ptr=ptr/2;//invalid Ptr1+ptr2; //invalid

Ex1: int a, b, c,*p1,*p2,*p3,*p4; a=2;b=3;c=4 p1=&a;p2=&b;p3=&c; *p4=a+*p2+*(&c)  ? Ex2: Int a,*p; P=&a; Printf(“enter the value of a”); Scanf(“%d”,&a); Scanf(“%d”,p); What is wrong in 2 nd scanf?

Pointers and functions Arguments are generally passed to functions by 1.Sending the values of arguments. 2.Sending the address of arguments. 1 st method(pass by value) Value of actual arguments in calling function is copied onto corresponding formal arguments in called function. Changes made are not reflected in actual arguments. Main() { int a=10,b=20; print(a);//10 print(b);//20

Swap(a,b); Print(a);//10 Print(b);//20 } Void swap(int c, int d) { int temp; temp=c; c=d; d=temp; } Here c==20 and d==10.but actual arguments not affected.(i.e value not changed in address.

2 nd method(pass by reference) Passing the address of variables. Changes are done in the memory. Hence gets reflected in main as well. Main() { int a=10,b=20; print(a);//10 print(b);//20 Swap(&a,&b); Print(a);//20 Print(b);//10 }

Void swap( int *c, int *d) { int temp; temp=*c; *c=*d; *d=temp; } Function returning pointers Just like a function can return an int, float, char etc, it can also return a pointer. Returning pointer means returning address of a variable.

Main() { int *fptr;//int pointer int *func();//function which returns int pointer fptr=func(); print(fptr);//2000 print(*fptr);//10 } Int *func() { static int a=10; return(&a); }

Pointer compatibility Main() { int a=10,*iptr, m; char b,*cptr; iptr=&a; cptr=&b; m=Sizeof(a);//2 m=Sizeof(iptr);//4 m=Sizeof(b);//1 m=Sizeof(cptr);//4 } Pointer of any data type(int,float,char) is 4 bytes long.

Even though pointers of different types are of same size (4 bytes),they cannot be assigned to each other. For ex:cptr=iptr or iptr=cptr are illegal. This is because these pointers point to different data types. Void pointer It is a pointer type to which any pointer can be assigned and which can be assigned to any pointer. void *pvoid; int *ptr1; char *ptr2; pvoid =ptr1; ptr2=pvoid;

Lvalue and Rvalue In c,an expression is either an l-value or r-value. L-value: This expression is used when the object is receiving a value. Usually used in the left side of ‘=‘ operator. ex: a=… b[5]=… *ptr=… R-value: Expression that can be used to supply a value. ex:…=10,….=a+2,….=a*6,…=a[5]+3,a++ A variable can act as both l-value or r-value; b=a;a=c;

Operators that need only L-value as operand are: &,++,--,= hence &(a+2);//error bcoz (a+2) is r-value &5;//error (a+2)++;//error ++(a+3);//error Pointers should be handled carefully because it actually accesses the memory location and improper use may harm your program and data.