Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.

Slides:



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

An introduction to pointers in c
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Programming and Data Structure
© 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.
Pointers in C Rohit Khokher
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Programming in C Pointer Basics.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Pointers CSE 2451 Rong Shi.
CHAPTER 7 DATA HANDALING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
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.
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:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Review 1 List Data Structure List operations List Implementation Array Linked List.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
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 IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
UNIT 8 Pointers.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
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.
Pointers What is the data type of pointer variables?
CS1010 Programming Methodology
CS1010 Programming Methodology
Functions and Pointers
Pointers and Pointer-Based Strings
Visit for more Learning Resources
Pointer.
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
DATA HANDLING.
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Topics discussed in this section:
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
POINTER CONCEPT 4/15/2019.
Data Structures and Algorithms Introduction to Pointers
Creating and Using Pointer Variables in C++ By: Ed Brunjes
POINTER CONCEPT 8/3/2019.
Presentation transcript:

Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND

Concepts of Pointers DEFINITION:The Pointer is a Variable which holds the Address of the other Variable in same memory. Such as Arrays, structures, and Functions that are used in program. It contains only the Memory Location of the variable rather than its content.

EXAMPLE FOR POINTER: int X = 547; int *ptr; Ptr=&X; HERE: Veriable nameContentsLocation X ptr According to above figure, By the help of ptr variable we stored the address of variable X in address 4036

Pointers are used in following variables Variables of any basic data type An Array Functions Structures, and Unions

Advantages of Pointers To point to different data structures To achieve clarity and simplicity More compact and efficient coding To return multiple values via functions Dynamic memory Allocation

Operators used with Pointers 1.The address operator & [ampersand] An address operator gives the address of the variable. 2.The indirection operator ‘*’ [asterisk] The indirection operator gives the value of the variable that the pointer is pointing to.

Pointer in Details POINTER CONSTANTS POINTER VALUES POINTER VARIABLES POINTERS

POINTER CONSTANTS: -Address with in computer are refers to the to as pointer constants use to store data values POINTER VALUES: -Address store in a pointer variable is refined as pointer value ( Address of variable ) POINTER VARIABLE: Variable that contains a pointer value is called a Pointer variable.

ACCESSING A VARIABLE THROUGH ITS POINTER We access the value of the variable by help of ‘pointer’ this is done by using another unary operator * (asterisk) usually known as “ Indirection operator “ consider following statements int quantity, *p,n; ( first line ) quantity = 179; (second line) p = & quantity; (third line) n = *p; (fourth line)

EXPLANATION: 1. The First line declares ‘quantity’ & ‘n’ as integer variable and ‘p’ as pointer variable 2.The second line Assigns value 179 to variable quantity 3.The third line assigns address of variable quantity to the pointer variable ‘p’ 4.The forth line contains ‘ * ’ operator in this case *p returns value of variable quantity Now value of ‘n’ would be 179 the Two statements p = & quantity; n = * p; Are Equivalent to n = * & quantity; which in turns is equivalent to n = quantity;

POINTER DECLARATION In ‘c’ Every Variable must be Declared its type,since pointer variables contain address of separate Data type, They must be Declared before use them the declaration of pointer variable takes the following form syntax: data _type * pt_ name EXAMPLE: 1. Int * p ; / * integer pointer */ & 2. float *p; /* float pointer */ HERE: 1.Declares the variable ‘p’ as a pointer variable that points to an integer data type 2. Declares the variable ‘p’ as a pointer variable that points to an ‘float’ data type

POINTER DECLARATION STYLES int* p ; // * style 1*// int *p ; //* style 2*// int * p ; //*style 3*// However,the style 2 is more popular due to following reasons; 1. This style is convenient to have multiple declaration in the same statement Ex: int *p, x,*q ; 2. This style matches with the format used for accessing the target values. Ex: int x, *p, y; x=10; y=*p; *p = 20;

PROGRAM EXAMPLE # include void main () { int ivar = 486; int * iptr ; iptr = & ivar; Printf(“ Address of ivar = %d\n”, iptr ); Printf (“ The content of ivar = %d\n”, * iptr); } OUT PUT: Address of ivar = 4056 The content of ivar = 486

POINTER INITIALIZATION As ordinary variables are Initialized with in Declaration part of the program, The Pointer variables can initialized by Accessing address of the variable that are Used in the program. Syntax : Data_type *ptr = expression Where: data_type = Any Basic Data type *ptr = pointer variable expression = another variable or may be constant

Example for pointer initialization Int quantity ; Int *p ; // * Declaration *// p = & quantity ; // * initialization*// Here; the third line assign the address of ‘quantity’ to pointer variable ‘p’

Invalid initializations 1. float a, b ; int x, * p ; p = & a ; // * wrong *// b = * p ; Here; we will get erroneous output because we are trying to assign the address of float variable to an integer variable it is not allowed in pointer assignments. 2.Int * p = & x, x ; // * wrong * // Here; it declares ‘ x ’ as integer variable, ‘p’ as pointer variable then assign ‘ p’ to the address of ‘x’. first we must be declare the ‘x’ before initialization hence above statement gives the erroneous output

PROGRAM EXAMPLE FOR POINTER INITIALIZATION # include Void main () int m,*ptr ; m = 270; ptr = & m; printf (“ The content of variable ‘m’ = %d\n”,*ptr); *ptr= 434 ; /* pointer initialization*/ Printf(“ the content of the variable ‘m’ after initialization = %d\n”, *ptr); 0UT PUT: The content of the variable ‘m’ = 270 The content of the variable ’m’ after initialization = 434

#include <iostream> Now that we know that the name of an array holds the address of the first member of the array, we realize that we can declare a pointer of the same data type as the array and initialize it with the array. Here is an example:int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int *pNumbers = Number; After this declaration and initialization, Number and pNumbers have the same value: int main() { int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int *pNumbers = Number; cout << "Addresses"; cout << "\n Number : " << Number; cout << "\npNumbers : " << pNumbers; return 0; } This would produce: Addresses Number : pNumbers : RELATING A POINTER TO AN ARRAY

As you may know, we can declare the form of a block of data containing different data types by means of a structure declaration. For example, a personnel file might contain structures which look something like: struct tag { char lname[20]; /* last name */ char fname[20]; /* first name */ int age; /* age */ float rate; /* e.g per hour */ }; POINTERS AND STRUCTURE

#include struct tag{ /* the structure type */ char lname[20]; /* last name */ char fname[20]; /* first name */ int age; /* age */ float rate; /* e.g per hour */ }; struct tag my_struct; /* define the structure */ void show_name(struct tag *p); /* function prototype */ int main(void) { struct tag *st_ptr; /* a pointer to a structure */ st_ptr = &my_struct; /* point the pointer to my_struct */ strcpy(my_struct.lname,"Jensen"); strcpy(my_struct.fname,"Ted"); printf("%s ",my_struct.fname); PROGRAM

void show_name(struct tag *p) { printf("%s ", p->fname); /* p points to a structure */ printf("%s ", p->lname); printf("%d", p->age); } printf("%s",my_struct.lname); my_struct.age = 63; show_name(st_ptr); /* pass the pointer */ return 0; }