Introduction to Pointers

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Programming and Data Structure
Pointers in C Rohit Khokher
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.
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.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
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.
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 What is the data type of pointer variables?
CS1010 Programming Methodology
CS1010 Programming Methodology
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
EGR 2261 Unit 10 Two-dimensional Arrays
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Pointers.
Pointers in C.
Pointers.
COSC 220 Computer Science II
Pointers and Pointer-Based Strings
Student Book An Introduction
Pointer Data Type and Pointer Variables II
Visit for more Learning Resources
CNG 140 C Programming (Lecture set 10)
Java Review: Reference Types
POINTERS.
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.
Lecture 6 C++ Programming
Pointers.
Tejalal Choudhary “C Programming from Scratch” Pointers
Buy book Online -
Object Oriented Programming COP3330 / CGS5409
Windows Programming Lecture 02.
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Pointers Kingdom of Saudi Arabia
Introduction to C++ Programming Language
Objectives You should be able to describe: Addresses and Pointers
Pointer Operations.
Overloading functions
Pointers Pointers point to memory locations
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
OPERATORS in C Programming
CISC181 Introduction to Computer Science Dr
OPERATORS in C Programming
Arrays and Pointers.
Introduction to Pointers
Presentation transcript:

Introduction to Pointers

pointer operator available in C is ‘ pointer operator available in C is ‘*’, called ‘value at address’ operator. It gives the value stored at a particular address. The ‘value at address’ operator is also called ‘indirection’ operator.

Note that printing the value of Note that printing the value of *( &i ) is same as printing the value of i. The expression &i gives the address of the variable i. This address can be collected in a variable, by saying, j = &i ;

But remember that j is not an ordinary variable like any other integer variable. It is a variable that contains the address of other variable (i in this case). Since j is a variable the compiler must provide it space in the memory. Once again, the following memory map would illustrate the contents of i and j.

But,we can’t use j in a program without declaring it But,we can’t use j in a program without declaring it. And since j is a variable that contains the address of i, it is declared as, int *j ; This declaration tells the compiler that j will be used to store the address of an integer value. In other words j points to an integer

Address of i = 65524 Address of j = 65522 Value of j = 65524 Value of i = 3 Value of i = 3 Value of i = 3

Pointer Arithmetic

Output:

Observations Observe the last three lines of the output. 65526 is original value in x plus 2, 65524 is original value in y plus 4, and 65520 is original value e in z plus 1. This so happens because every time a pointer is incremented it points to the immediately next location of its type. That is why, when the integer pointer x is incremented, it points to an address two locations after the current location, since an int is always 2 bytes long . Similarly, y points to an address 4 locations after the current location and z points 1 location after the current location

The way a pointer can be incremented, it can be decremented as well, to point to earlier locations. Thus, the following operations can be performed on a pointer: Addition of a number to a pointer. For example, int i = 4, *j, *k ; j = &i ; j = j + 1 ; j = j + 9 ; k = j + 3 ;

Subtraction of a number from a pointer. For example, int i = 4, *j, *k ; j = &i ; j = j - 2 ; j = j - 5 ; k = j - 6

Subtraction of one pointer from another. One pointer variable can be subtracted from another provided both variables point to elements of the same array.

Suppose the array begins at location 65502, then the elements arr[1] and arr[5] would be present at locations 65504 and 65512 respectively, since each integer in the array occupies two bytes in memory. The expression j - i would print a value 4 and not 8. This is because j and i are pointing to locations that are 4 integers apart. What would be the result of the expression *j - *i? 36, since *j and *i return the values present at addresses contained in the pointers j and i.

Comparison of two pointer variables Pointer variables can be compared provided both variables point to objects of the same data type. Such comparisons can be useful when both pointer variables point to elements of the same array. The comparison can test for either equality or inequality. Moreover, a pointer variable can be compared with zero (usually expressed as NULL). The following program illustrates how the comparison is carried out.

Do not attempt the following operations on pointers Do not attempt the following operations on pointers... they would never work out. (a) Addition of two pointers (b) Multiplication of a pointer with a constant (c) Division of a pointer with a constant

Pointers and Functions Passing pointer as function parameter Function returning Pointer Pointer to functions or Function pointer

Function returning a pointer A function can also return a pointer to the calling function

Function pointer Function pointer : A pointer which keeps address of a function is known as function pointer

#include<stdio.h> void display(); int main() { void (*ptr)(); ptr = &display; (*ptr)(); return(0); } void display() printf("Hello World");

Consider Scenario using pointer , We should follow following 3 steps to use pointer to call function – Declare Pointer which is capable of storing address of function. Initialize Pointer Variable Call function using Pointer Variable.

Step 1 : Declaring Pointer void (*ptr)();We are simply declaring a double pointer. Write () symbol after “Pointer“. void represents that , function is not returning any value. () represents that , function is not taking any parameter.