Pointer. Accessing address of a variable POINTER.

Similar documents
Data Structures using OOP C++ Lecture 1

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Introduction to Java

An Incomplete C++ Primer. University of Wyoming MA 5310

Chapter 13 Storage classes

Implementation Aspects of OO-Languages

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Pemrograman Dasar. Basic Elements Of Java

Keil C51 Cross Compiler

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1 Abstract Data Types Information Hiding

Passing 1D arrays to functions.

Stacks. Linear data structures

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Object Oriented Software Design II

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

The C Programming Language course syllabus associate level

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Semantic Analysis: Types and Type Checking

Illustration 1: Diagram of program function and data flow

Chapter 5 Names, Bindings, Type Checking, and Scopes

Sources: On the Web: Slides will be available on:

C++ Language Tutorial

Conditionals (with solutions)

Chapter 2: Elements of Java

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Answers to Review Questions Chapter 7

Introduction to Data Structures

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Informatica e Sistemi in Tempo Reale

Introduction to Java. CS 3: Computer Programming in Java

C++ INTERVIEW QUESTIONS

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

arrays C Programming Language - Arrays

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Comp151. Definitions & Declarations

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java (12 Weeks) Introduction to Java Programming Language

C++FA 5.1 PRACTICE MID-TERM EXAM

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

An Overview of Java. overview-1

Lecture 22: C Programming 4 Embedded Systems

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

El Dorado Union High School District Educational Services

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Chapter 7D The Java Virtual Machine

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Object Oriented Software Design II

The programming language C. sws1 1

Habanero Extreme Scale Software Research Project

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Example of a Java program

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Java Basics: Data Types, Variables, and Loops

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

Tutorial on C Language Programming

7.1 Our Current Model

Chapter 5 Functions. Introducing Functions

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Subject Name: Object Oriented Programming in C++ Subject Code:

C++ Programming Language

A brief introduction to C++ and Interfacing with Excel

Chapter One Introduction to Programming

DNA Data and Program Representation. Alexandre David

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Number Representation

VB.NET Programming Fundamentals

C++FA 3.1 OPTIMIZING C++

J a v a Quiz (Unit 3, Test 0 Practice)

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Arrays in Java. Working with Arrays

Java Interview Questions and Answers

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

PES Institute of Technology-BSC QUESTION BANK

Pseudo code Tutorial and Exercises Teacher s Version

C++ Overloading, Constructors, Assignment operator

C++ Essentials. Sharam Hekmat PragSoft Corporation

Transcription:

www.cppforschool.com Pointer Accessing address of a variable Computer s memory is organized as a linear collection of bytes. Every byte in the computer s memory has an address. Each variable in program is stored at a unique address. We can use address operator & to get address of a variable: int num = 23; cout << &num; // prints address in hexadecimal POINTER A pointer is a variable that holds a memory address, usually the location of another variable in memory. Defining a Pointer Variable int *iptr; iptr can hold the address of an int Pointer Variables Assignment: int num = 25; int *iptr; iptr = &num; Memory layout To access num using iptr and indirection operator * cout << iptr; // prints 0x4a00 cout << *itptr; // prints 25 Similary, following declaration shows: char *cptr; float *fptr; cptr is a pointer to character and fptr is a pointer to float value.

Pointer Arithmetic Some arithmetic operators can be used with pointers: - Increment and decrement operators ++, -- - Integers can be added to or subtracted from pointers using the operators +, -, +=, and -= Each time a pointer is incremented by 1, it points to the memory location of the next element of its base type. If p is a character pointer then p++ will increment p by 1 byte. If p were an integer pointer its value on p++ would be incremented by 4 bytes. Pointers and Arrays Array name is base address of array int vals[] = 4, 7, 11; cout << vals; // displays 0x4a00 cout << vals[0]; // displays 4 Lets takes an example: int arr[]=4,7,11; int *ptr = arr; What is ptr + 1? It means (address in ptr) + (1 * size of an int) cout << *(ptr+1); // displays 7 cout << *(ptr+2); // displays 11 Array Access Array notation arr[i] is equivalent to the pointer notation *(arr + i) Assume the variable definitions int arr[]=4,7,11; int *ptr = arr; Examples of use of ++ and -- ptr++; // points at 7 ptr--; // now points at 4

Character Pointers and Strings Initialize to a character string. char* a = Hello ; a is pointer to the memory location where H is stored. Here a can be viewed as a character array of size 6, the only difference being that a can be reassigned another memory location. char* a = Hello ; a gives address of H *a gives H a[0] gives H a++ gives address of e *a++ gives e Pointers as Function Parameters A pointer can be a parameter. It works like a reference parameter to allow change to argument from within function #include<iostream> using namespace std; void swap(int *, int *); int main() int a=10,b=20; swap(&a, &b); cout<<a<<" "<<b; return 0; void swap(int *x, int *y) int t; t=*x; *x=*y; *y=t; output: 20 10

Pointers to Constants and Constant Pointers Pointer to a constant: cannot change the value that is pointed at Constant pointer: address in pointer cannot change once pointer is initialized Pointers to Structures We can create pointers to structure variables struct Student int rollno; float fees;; Student stu1; Student *stuptr = &stu1; (*stuptr).rollno= 104; -or- Use the form ptr->member: stuptr->rollno = 104; Static allocation of memory In the static memory allocation, the amount of memory to be allocated is predicted and preknown. This memory is allocated during the compilation itself. All the declared variables declared normally, are allocated memory statically. Dynamic allocation of memory In the dynamic memory allocation, the amount of memory to be allocated is not known. This memory is allocated during run-time as and when required. The memory is dynamically allocated using new operator. Free store Free store is a pool of unallocated heap memory given to a program that is used by the program for dynamic allocation during execution. Dynamic Memory Allocation We can allocate storage for a variable while program is running by using new operator

To allocate memory of type integer int *iptr=new int; To allocate array double *dptr = new double[25]; To allocate dynamic structure variables or objects Student sptr = new Student; //Student is tag name of structure Releasing Dynamic Memory Use delete to free dynamic memory delete iptr; To free dynamic array memory delete [] dptr; To free dynamic structure delete Student; Memory Leak If the objects, that are allocated memory dynamically, are not deleted using delete, the memory block remains occupied even at the end of the program. Such memory blocks are known as orphaned memory blocks. These orphaned memory blocks when increase in number, bring adverse effect on the system. This situation is called memory leak Self Referential Structure The self referential structures are structures that include an element that is a pointer to another structure of the same type. struct node int data; node* next;