Pointer Arithmetic By Anand George.

Slides:



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

Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Branches Two branch instructions:
Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
How Create a C++ Program. #include using namespace std; void main() { cout
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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:
Mixing integer and floating point numbers in an arithmetic operation.
Pointers *, &, array similarities, functions, sizeof.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
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.
Intro to Pointers in C CSSE 332 Operating Systems
Stack and Heap Memory Stack resident variables include:
“Studying C programming excluding pointers is meaningless.” d0m3z
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Pointers.
Introduction to Programming
C/C++: type sizes in memory pointers
Chapter 12 Variables and Operators
EPSII 59:006 Spring 2004.
Pointers and Pointer-Based Strings
Programmazione I a.a. 2017/2018.
void Pointers Lesson xx
Pointers and References
Pointers  Week 10.
Introduction to Problem Solving and Programming
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Lecture 2 SCOPE – Local and Global variables
Initializing variables
C Programming Getting started Variables Basic C operators Conditionals
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
C Language B. DHIVYA 17PCA140 II MCA.
CSCE 206 Lab Structured Programming in C
Structures in c By Anand George.
Pointers and References
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
C Pointers Systems Programming.
Functions By Anand George.
Introduction to Pointers
Presentation transcript:

Pointer Arithmetic By Anand George

Pointer types Classification based on the type of data it points Types of pointers Pointer to Data Pointers to functions ( code ) All pointers are of same size 32 bit in a 32 bit OS respective of what type it is.

Pointers to data Different types int * char * float* ACustomStructure* All 32 bit size.. Then what is the difference?

Difference is.. The number of locations the pointer jumps when you add 1 ( or any number for that matter) to the pointer variable. The pointer arithmetic is counter intuitive ( some time you get 1+1 = 2, some times 3, 4 like that ) and depends on the type of data.

What does that mean? Suppose int *ptr = 100; Now the address inside variable ptr = 100; now what is ptr + 1 in a 32 bit OS? intuitive answer is 101 but actual answer is 104 ( assuming int is 4 byte )

What is the reason? by the statement ptr+1 we are telling the compiler “hey compiler I'm already pointing to an integer (int *ptr ) and now I want to point to the NEXT INTEGER ( ptr+1 )” and NOT the next memory location. Next integer is 4 bytes away so compiler has to add 4 instead of 1 to go to NEXT integer.

Demo #include <stdio.h> void main() { } int *ptrA = (int*)100; int *ptrResult = ptrA + 1; printf("ptrA = %d, ptrResult = %d ", ptrA, ptrResult ); }

Houses in a street

Jump is equal to sizeof ( data type) char* jump is 1 int* jump is 4 sizeof operator pointer arithmetic has only – and + no division, multiplication or any other operation.

Demo Allocating a chunk and accessing it with pointer arithmetic. Using different types of pointer. char, int ,short Using * to dereference the pointer variable. sizeof operator

Summary Pointer types based on size of data it points to int* char* Difference between the pointer types is the amount it jumps when arithmetic is done. sizeof operator.

Thank You