Pointer Operations.

Slides:



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

UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Java Data Types Assignment and Simple Arithmetic.
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:
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Variables, Operators, and Expressions
Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Pointers.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Primer 1: Types, Classes and Operators
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers in C.
Chapter 7: Expressions and Assignment Statements
Student Book An Introduction
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Tejalal Choudhary “C Programming from Scratch” Pointers
Operators and Expressions
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Object Oriented Programming COP3330 / CGS5409
Explicit and Implicit Type Changes
Lecture 3 Expressions Richard Gesick.
Instructor: Ioannis A. Vetsikas
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Pointers.
Chapter-3 Operators.
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
elementary programming
Initializing variables
Chapter 3 Operators and Expressions
Data Types and Expressions
Pointers Pointers point to memory locations
Data Structures and Algorithms Introduction to Pointers
Java Programming Language
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Operator King Saud University
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
Introduction to Pointers
Data Types and Expressions
Presentation transcript:

Pointer Operations

Type Casting Type Casting: Converting the data type of one variable into another data type. Syntax: (data type to be converted to) variable; Eg: float a = 2.3; int b; b=(int)a; Types: Implicit Conversion: int float double (small no of bytes higher no of bytes) Explicit Conversion: double float int (higher no of bytes small no of bytes)

Pointer Conversions Converting pointer from one type to another: Pointer of one type cannot be implicitly converted from one type to another but can be explicitly converted using type casting. In such a conversion a pointer always assumes that it is point to a object of its type but reality may differ. Syntax: (data type to be converted to *) variable; int *p; (char *)p; Normal variable cannot be converted to pointer variable using type casting. Pointer variable of one type can be type casted to another type.

Pointer Conversions Notes: Type conversion is a powerful feature but yet it may difficult to remove bugs and crashes and should be used with uttermost vigilance. It may also lead to unexpected and unreliable results but program would compile successfully. While typecasting one pointer to another because even after type casting the pointer can point to anything but it will still think it is pointing to something of it declared type and have properties of the original type. void pointer can be used for this purpose. As the void pointer doesn’t point to any type of data, it can be type casted to any type, and results in no error.

Pointer Conversions Example program to convert the pointer from pointing to int to char data type. main() { int i = 10; char *p1; int *p2; p2 = &i; p1 = (char *) p2; // Type Casting and Pointer Conversion printf (" *p1 = %c And *p2 = %d", *p1,*p2); // Output will be depending upon the compiler. }

Pointer Conversions Example program to convert the pointer from pointing to void to char data type. main() { int i = 10; int *p1; void *p2; p2 = &i; p1 = (int *) p2; // Type Casting and Pointer Conversion printf (" *p1 = %d And *p2 = %d", *p1,*p2); } Output: *p1 = 10 And *p2 = 10

Pointer Arithmetic int a=13; 2 bytes as a whole form the 65345 65346 integer float q=2.3; float *s; s=&q; *s will give the value 2.3 4 bytes form the 23456 25457 25458 25459 float value Pointer variable ‘s’ will be assigned with the address of q. The address of q assigned will be 23456. As the given type is float, *s will take the value as a whole from four bytes. 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits

Pointer Arithmetic Notes: When the address of a variable is assigned to a pointer variable, only the starting byte’s address will be stored. According to the data type the no of bytes from the starting bytes will be taken account. If the data type is not proper, pointer variable will not be knowing till which byte the data is stored. The arithmetic operations performed on the pointer variable, it must be applied such that it is able to retrieve the whole value whether it is an integer or float or char etc.,

Pointer Arithmetic When a pointer of certain base type is increased, it increases its value in such a way that it points to next element of its base type. If a pointer of certain base type is decremented, its value decreases in such a way that it points to previous value of its base type. Increment as well as decrement in fixed quanta of size of the base type. Pointer value can be incremented or decremented only by integer as it is holding the address. Two forms of pointer arithmetic: Pointer + integer Pointer – pointer (Will dealt later while dealing with array and pointers

Pointer Arithmetic Unary Pointer Arithmetic Operators: Pointer variable ++; Adds sizeof(datatype) number of bytes to pointer, so that it points to the next entry of the datatype. The no of bytes is determined by the data type of the value the pointer variable is pointing to. Pointer variable --; Subtracts sizeof(datatype) number of bytes to pointer, so that it points to the next entry of the datatype.

Pointer Arithmetic Example of incrementing the float type pointer. 23456 23457 23458 23459 23460 23461 23462 ------- 4 bytes form the float value Incrementing the pointer will make the pointer to point to starting of next 4 bytes main() { float a = 2.3, *b=&a; printf(“Address of á’ stored in b:”, b); printf(“Value stored in a:”, *b); printf(“Incrementing the pointer variable: ”, ++b); printf(“Value retrieved after incrementing:”, *b); } Output: Address of á’ stored in b: 23456 Value stored in a: 2.3 Incrementing the pointer variable: 23460 Value retrieved after incrementing: // Not known. Will be retrieved at the execution time. 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits

Pointer Arithmetic main() { int *ptrn; int a; float *ptrflt; float b; ptrn=&a; ptrflt=&b; ptrn++; //increments by sizeof(int) (2 bytes) ptrflt--; //increments by sizeof(long) (4 bytes) }

Pointer Arithmetic Arithmetic Operations between a pointer and an integer: Pointer + integer Pointer Variable + n is valid, if n is an integer. The result is the following: Pointer Variable + (n*sizeof(data type of the value pointer points to)) It advances the pointer by n number of size of data type. Pointer Variable - n is similar. The result will be: Pointer Variable - (n*sizeof(data type of the value pointer points to)) It decrements the pointer by n number of size of data type.

Pointer Arithmetic Eg: float h=4.6, *ptrf; ptrf=&h; If h is stored at address 23454, then ptrf will be having the address value 23454 stored in it. Pointer Increment Value Incremented Pointing address Value retrieved ptrf - 23454 4.6 ptrf+1 // ptrf++ ptrf+(1*sizeof(float)) 23458 Points to the next four bytes starting from 23458 ptrf+2 ptrf+(2*sizeof(float)) 23462 Points to the next four bytes starting from 23462 ptrf+3 Ptrf+(3*sizeof(float)) 23466 Points to the next four bytes starting from 23466

Pointer Arithmetic Notes: Arithmetic operations addition (or) subtraction of an integer value can be done on a pointer. Multiplication, Division, Modulus by an integer cannot be done on a pointer. This will give the error Illegal use of a pointer in function main. Addition of two pointers cannot be done. This will give the error Invalid pointer addition.

Pointer Comparison Pointer variable can be compared only with another pointer. Comparison can be done using <, >, ==, <= and >= operators. When two pointers are compared, actually the address they are holding is compared. Using ‘==’ operator on pointers, will check whether both pointers being compared are pointing to the same address or not If pointer a and pointer b are holding the same address, then a==b will be true. Otherwise false. The use arithmetic operations and the comparisons will be having wide space when the pointer is applied to array. A pointer can be checked whether it is pointing to NULL using the ‘==’ (Equal to) operator int *p=NULL; if(p==0 ) will return true.

Pointer Comparison Valid Comparisons: Comparing same type pointers. Comparing pointers pointing to same location. Comparison can be done to check for NULL pointer Invalid Comparisons: Comparing pointer and a normal variable. // Compiler Error Comparing different type of pointers. Such as comparing char pointer with int or some other type of pointer. // Warning