Pointer Data Type and Pointer Variables

Similar documents
Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS201- Introduction to Programming Current Quizzes

Homework #3 CS2255 Fall 2012

C++ for Java Programmers

Pointers II. Class 31


Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

DECLARAING AND INITIALIZING POINTERS

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

Pointers, Dynamic Data, and Reference Types

PROGRAMMAZIONE I A.A. 2017/2018

Computer Programming

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Lectures 6/7 Pointers and Dynamic Arrays

CSC 211 Intermediate Programming. Pointers

Dynamic Allocation of Memory

CS242 COMPUTER PROGRAMMING

FORM 2 (Please put your name and form # on the scantron!!!!)

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Exam 3 Chapters 7 & 9

UNIT- 3 Introduction to C++

CS2255 HOMEWORK #1 Fall 2012

[0569] p 0318 garbage

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter 7 - Notes User-Defined Functions II

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

CS2141 Software Development using C/C++ C++ Basics

Syntax to define a Structure: struct structurename { datatype membername1; datatype membername2;... } ; For Example:

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

What have we learned about when we learned about function parameters? 1-1


Pointers. Variable Declaration. Chapter 10

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

1. In C++, reserved words are the same as predefined identifiers. a. True

Variation of Pointers

6. C++ Subprograms David Keil CS I slides 7/03 1

For Teacher's Use Only Q No Total Q No Q No

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Short Notes of CS201

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

CS201 - Introduction to Programming Glossary By

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

C++ Final Exam 2017/2018

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Come and join us at WebLyceum

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

pointers + memory double x; string a; int x; main overhead int y; main overhead

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CMSC 202 Midterm Exam 1 Fall 2015

Chapter 9. Pointers and Dynamic Arrays

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Computer Programming Lecture 12 Pointers

BITG 1113: Function (Part 2) LECTURE 5

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

Lab 3. Pointers Programming Lab (Using C) XU Silei

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

CS201 Some Important Definitions

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Creating a C++ Program

Introduction to Computer Science Midterm 3 Fall, Points

First of all, it is a variable, just like other variables you studied

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Informatica 3 Syntax and Semantics

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Procedural programming with C

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

a data type is Types

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Arrays. int Data [8] [0] [1] [2] [3] [4] [5] [6] [7]

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 11: Pointers

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Pointers and Dynamic Arrays

Constants, References

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Lecture 3 Tao Wang 1

CSCE 110 PROGRAMMING FUNDAMENTALS

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Scope and Parameter Passing

Module Operator Overloading and Type Conversion. Table of Contents

C++ 8. Constructors and Destructors

Transcription:

Pointer Data Type and Pointer Variables Pointer variable: content is a memory address There is no name associated with the pointer data type in C++. Declaring Pointer Variables Syntax: Data-type *identifier Examples: int *p; char *ch; These statements are equivalent: int *p; int* p; int * p; Dr. Hussein H. Owaied Business Networking and Systems Management Philadelphia University Amman-Jordan

Declaring Pointer Variables In the statement: int* p, q; only p is the pointer variable, not q; here q is an int variable To avoid confusion, attach the character * to the variable name: int *p, q; int *p, *q; Address of Operator (&) The ampersand, &, is called the address of operator The address of operator is a unary operator that returns the address of its operand. Dereferencing Operator (*) When used as a unary operator, * is the dereferencing operator or indirection operator Refers to object to which its operand points

Application of Operator (&) and (*) When used as a unary operator, * is the dereferencing operator or indirection operator Refers to object to which its operand points Example 1: Int x=5; Int *p; P=&x; //store the address of x in p To print the value of x, using p: Cout << *p << endl; //will print 5 To store a value in x, using p: *p =25; //Now x=25

//Program 1 Arrays and Pointers //Manipulation of an array through a pointer char cstr[28] = "Welcome to wherever you are."; #include<iostream.h> void main() { char *cptr; cout<<"the string is: "<<cstr<<endl; cptr = cstr; //cptr is pointing to wherever cstr is pointing to cout<<"the string is: "<<cptr<<endl; } The output of Program 1 is: The string is Welcome to wherever you are. The string is Welcome to wherever you are.

Explanation of using pointer Although Program 1 gives an impression that arrays and pointers are the same, there is a subtle difference between the two. Both cstr and cptr are pointers to char data type. However, cstr as a pointer is constant, which means it always points to the first element in the array of 28 characters. Therefore, the value of cstr cannot be changed. But cptr as a pointer is dynamic. It can be made to any address in memory. To understand the concept better, consider the following example:

Continue Explanation of using pointer //Program 2 //This program demonstrates the dynamic nature of pointers #include<iostream.h> char cstr[] = "Mad dogs go to heaven"; void main() { char *cptr, cvar = 'A'; cptr = cstr; //cptr and cstr both point to the same location, that is the first element of the array cstr cout<<cptr<<endl; cptr = &cvar; //Now cptr is pointing to the variable cvar cout<<*cptr; } The output of Program 2 is: Mad dogs go to heaven A

Dynamic Memory Allocation The main drawback of arrays is that the size of the arrays is fixed and it needs to be specified at compile time. Memory can be deallocated and reallocated dynamically by using the new and the delete operators. new Operator Pointers provide the necessary support for C++'s powerful dynamic memory allocation system. Dynamic allocation is the means by which a program can obtain memory while it is running. The syntax for the new operator is: <variable> = new <type>; where <variable> is a pointer variable and <type> can be char, int, float or any user defined data type. The type of variable mentioned on the left hand side and the type mentioned on the right hand side should match. Consider the following examples:

Examples for new and delete operators char *cptr; int *iptr; cptr = new char[10]; // This declaration allocates ten bytes to the pointer cptr. iptr = new int; // This declaration allocates four bytes of memory // and assigns the starting address to iptr. The syntax of the new operator can also be modified to allocate memory of varying requirements. For example, char *cprt; //allocates five bytes of memory and assigns the starting address to cptr. cptr = new char[5]; delete Operator The delete operator is used to release the memory, which was allocated, using the new operator. The syntax of the delete operator is: delete <variable>; where <variable> is a pointer variable. Consider the following example: char *ps; ps = new char[10]; delete ps; //The above code releases the allocated memory to the pointer ps

Modular Approach Functions are the building blocks of C++ programs. A function groups a number of program statements into a single unit. This unit can be invoked from other parts of the program. Therefore we need a function. A group of functions together form a large entity called a module. There are may reasons for using function such as: 1. When programs become large, a single list of instruction becomes difficult to understand. 2. A function has a cleared defined purpose and has an interface to other functions in the program. 3. Using functions is to reduce the size of the program. Any sequence of instructions that is repeated in a program can be grouped together to form a function. The function code is stored in only one place in the memory even though the function may be executed a repeated number of times.

How we deal with Function There are three concepts should be considered: 1. Function Prototype or Function Declaration 2. Function Definition 3. Function Call Statement Function Prototype or Function Declaration The syntax of function prototype Function Declaration is: Data-type function-name(formal-parameters-list); Formal-parameters-list means the formal-data-types of the function parameters. Formal-parameters-list >= nill Examples: int sum(int,int); char str(char, char); int add();

Function Definition After writing function prototype must define it. The syntax of definition a function is: Data-type function-name(formal-1 var1, formal-2 var2, formal-3 var3,..) { Function Body } Example: int sum(int x, int y) { double z; cin >>x; cin >>y; z=x+y; cout <<z; }

Function Call Statement The syntax of function call statement is: Function-name(actual-parameters-list); Example : Sum(5,9); //this is function call statement Where 5,9 are actual parameters Note: function call without data-type of function