POINTER CONCEPT 8/3/2019.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
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 Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
What are Pointers? Different from other normal variables which can store values. pointers are special variables that can hold the address of a variable.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
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.
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.
POINTERS.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
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.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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)
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
CS1010 Programming Methodology
Chapter 8 Arrays, Strings and Pointers
Chapter 7 Pointers and C-Strings
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
BILASPUR UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE AND APPLICATION
Functions and Pointers
Pointers.
Pointers and Pointer-Based Strings
Visit for more Learning Resources
CSI-121 Structured Programming Language Lecture 16 Pointers
Andy Wang Object Oriented Programming in C++ COP 3330
Functions and Pointers
DATA HANDLING.
Pointer Basics Psst… over there.
Buy book Online -
Arrays, For loop While loop Do while loop
Pointers, Dynamic Data, and Reference Types
Constant pointers and pointers to constants
Outline Defining and using Pointers Operations on pointers
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
POINTER CONCEPT 4/15/2019.
Chapter 9: Pointers and String
Pointer Basics Psst… over there.
Variables and Constants
Data in a C++ Program Numeric data and character data
SPL – PS2 C++ Memory Handling.
Getting Started With Coding
Presentation transcript:

POINTER CONCEPT 8/3/2019

Through pointers a developer can directly access memory from his/her code which makes memory related operations very fast. But, as always, with great power comes great responsibility.

What are Pointers? Different from other normal variables which can store values, pointers are special variables that can hold the address of a variable. Since they store memory address of a variable, the pointers are very commonly said to “point to variables”.

A normal variable ‘var’ has a memory address of 1001 and holds a value 50. A pointer variable has its own address 2047 but stores 1001, which is the address of the variable ‘var’

How to Declare a Pointer? A pointer is declared as : <pointer type> *<pointer-name> pointer-type : It specifies the type of pointer. It can be int,char, float etc. This type specifies the type of variable whose address this pointer can store. pointer-name : It can be any name specified by the user. Professionally, there are some coding styles which every code follows. The pointer names commonly start with ‘p’ or end with ‘ptr’

char *chptr; In the above declaration, ‘char’ signifies the pointer type, chptr is the name of the pointer while the asterisk ‘*’ signifies that ‘chptr’ is a pointer variable.

How to initialize a Pointer? pointer declaration(except semicolon)> = <address of a variable> OR <pointer declaration> <name-of-pointer> = <address of a variable> Note that the type of variable above should be same as the pointer type

Example : char ch = 'c'; char *chptr = &ch; //initialize OR char *chptr; chptr = &ch //initialize

In the code above, we declared a character variable ch which stores the value ‘c’. Now, we declared a character pointer ‘chptr’ and initialized it with the address of variable ‘ch’. Note that the ‘&’ operator is used to access the address of any type of variable.

How to Use a Pointer? A pointer can be used in two contexts. Context 1: For accessing the address of the variable whose memory address the pointer stores. char ch = 'c'; char *chptr = &ch; Now, whenever we refer the name ‘chptr’ in the code after the above two lines, then compiler would try to fetch the value contained by this pointer variable, which is the address of the variable (ch) to which the pointer points

Context 2: For accessing the value of the variable whose memory address the pointer stores char ch = 'c'; char t; char *chptr = &ch; t = *chptr; ‘*chptr’ yeilds ‘c’. the asterisk ‘*’ operator is also known as ‘value of’ operator.

#include <stdio.h> int main(void) { char ch = 'c'; char *chptr = &ch; int i = 20; int *intptr = &i; float f = 1.20000; float *fptr = &f; char *ptr = "I am a string"; printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr); return 0; }

C Constant pointer char ch, c; char *ptr = &ch ptr = &c First, the pointer ‘ptr’ contained the address of ‘ch’ and in the next line it contained the address of ‘c’ But in case of a constant pointer, once a pointer holds an address, it cannot change it. third line would have not been valid.

<type-of-pointer> <type-of-pointer> *const <name-of-pointer> EX: #include<stdio.h> int main(void) { char ch = 'c'; char c = 'a'; char *const ptr = &ch; // A constant pointer ptr = &c; // Trying to assign new address to a constant pointer. WRONG!!!! return 0; }