DECLARAING AND INITIALIZING POINTERS

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

Unit 7. Functions. Need of User Defined Functions

Language comparison. C has pointers. Java has references. C++ has pointers and references

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

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Computer Programming Lecture 13 Functions with Multiple Output Parameters

Assist. Prof. Dr. Caner ÖZCAN

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

C++ for Java Programmers

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

BSM540 Basics of C Language

PROGRAMMAZIONE I A.A. 2017/2018

by Pearson Education, Inc. All Rights Reserved.

[0569] p 0318 garbage

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Variation of Pointers

At the end of this module, the student should be able to:

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

Algorithms & Data Structures

15 FUNCTIONS IN C 15.1 INTRODUCTION

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

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

Pointers. 10/5/07 Pointers 1

Lecture 2: C Programming Basic

Pointer Data Type and Pointer Variables

Goals of this Lecture

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Problem Solving and 'C' Programming

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

POINTER & REFERENCE VARIABLES

CS240: Programming in C

Computers Programming Course 5. Iulian Năstac

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Pointers. Introduction

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

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Computer Programming

Programming for Engineers Introduction to C

Pointers and Arrays 1

Chapter 2, Part I Introduction to C Programming

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Pointers and File Handling

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Object oriented programming C++

Fundamentals of Programming Session 12

UNIT - V STRUCTURES AND UNIONS

(2½ Hours) [Total Marks: 75

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Chapter 7 C Pointers

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

MYcsvtu Notes LECTURE 34. POINTERS

Final CSE 131B Spring 2005

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

Array Initialization

Pointers. Memory. void foo() { }//return

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);

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

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Understanding Pointers

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Introduction to C. Systems Programming Concepts

Dynamic Memory Allocation

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Introduction to Scientific Computing and Problem Solving

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

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Lecture 04 Introduction to pointers

Pointers as Arguments

CS C Primer. Tyler Szepesi. January 16, 2013

Programming for Electrical and Computer Engineers. Pointers and Arrays

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Function I/O. Last Updated 10/30/18

CS201- Introduction to Programming Current Quizzes

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

Assembler Programming. Lecture 10

Lecture 3: C Programm

CC112 Structured Programming

QUIZ on Ch.8. What is kit?

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

Work relative to other classes

Dynamic Memory Allocation and Command-line Arguments

ESC101N Fundamentals of Computing

Programming Languages

Transcription:

DECLARAING AND INITIALIZING POINTERS Passing arguments Call by Address

Introduction to Pointers Within the computer s memory, every stored data item occupies one or more contiguous memory cells (i.e., adjacent bytes). The number of cells required to store a data item depends on the type of data item. For example, a single character will typically be stored in one byte ( 8 bits ) of memory; an integer usually requires two contiguous bytes; a floating-point number may require four contiguous bytes.

Introduction to Pointers Suppose v is a variable that represents some particular data item. The compiler will automatically assign memory cells for this data item. The data item can then be accessed if we know the location ( i.e., the address) of the first memory cell. The address of v s memory location can be determined by the expression &v, where & is a unary operator, called the address operator, that evaluates the address of its operand.

Introduction to Pointers Now let us assign the address of v to another variable, pv. Thus, pv = &v; This new variable is called a pointer to v, since it points to the location where v is stored in memory. Remember, however, that pv represents v s address, not its value. Thus pv is referred to as a pointer variable.

Introduction to Pointers The relationship between pv and v is illustrated in Fig. address of v pv value of v v Relationship between pv and v ( where pv = &v and v = *pv)

Introduction to Pointers So a pointer is a variable that represents the location ( rather than the value ) of a data item, such as a variable or an array element.

Pointer Declarations Pointer variables, like all other variables, must be declared before they may be used in a C program. The interpretation of a pointer declarations differs, however, from the interpretation of other variable declarations. When a pointer variable is declared, the variable name must be preceded by an asterisk ( * ). This identifies the fact that variable is a pointer.

Pointer Declarations The data type that appears in the declaration refers to the object of the pointer, i.e., the data item that is stored in the address represented by the pointer, rather than the pointer itself. Thus, a pointer declaration may be written in general terms as data-type *ptvar; Where ptvar is the name of the pointer variable, and data type refers to the data type of the pointer s object.

Passing Arguments (Pointers) to a Function Call By Address Pointers are often passed to a function as arguments. This allows data items within the calling portion of the program to be accessed by the function, altered within the function, and then returned to the calling portion of the program in altered form. We refer this use of pointers as passing arguments by reference ( or by address or by location), in contrast to passing arguments by value as discussed earlier.

Passing Arguments (Pointers) to a Function Call By Address When an argument is passed by value, the data item is copied to the function. Thus, any alteration made to the data item within the function is not carried over into the calling routine. When an argument is passed by reference, however, the address of data item is passed to the function. The contents of that address can be accessed freely, either within the function or within the calling routine.

Passing Arguments (Pointers) to a Function Call By Address Moreover, any change that is made to the data item ( i.e., to the contents of the address) will be recognized in both the function and the calling routine. Thus, the use of a pointer as a function argument permits the corresponding data item to be altered globally from within the function.

Passing Arguments (Pointers) to a Function Call By Address When pointers are used as arguments to a function, some care is required with the formal argument declarations within the function. Specifically, formal pointer arguments that must each be preceded by an asterisk.

Passing Arguments to a Function Call By Address - Example Here is a simple C program containing a function that alters the value of its argument: # include <stdio.h> void modify ( int *ptr ); void main (void) { int a = 2; printf ( \na=%d ( from main, before calling the function),a); modify(&a); printf ( \na=%d ( from main, after calling the function),a); } void modify ( int *ptr) { *ptr += 3; printf( \n\na = %d ( from the function, after being modified ), *ptr); return; }

Passing Arguments to a Function Call By Address - Example The original value of a ( i.e., a = 2) is displayed when main begins execution. The address of variable a is then passed to the function modify, where 3 is added 3 to it and the new value will be displayed.

Passing Arguments to a Function Call By Address - Example Note that it is the altered value of the formal argument that is displayed within the function. Finally, the value of a within main ( i.e., the actual argument) is again displayed, after control is transferred back to the main from modify.

Passing Arguments to a Function Call By Address - Example When the program is executed, the following output is generated: a =2 (from main, before calling the function) a =5 ( from the function, after being modified ) a =5 (from main, after calling the function) These results show that a is altered within the main, and the corresponding value of a is changed within modify.

Passing Arguments to a Function Call By Address - Important The following points must be noted about passing arguments using call by address mechanism: The actual arguments can only be variables. When the control is transferred from the calling function to the called function, the memory for formal arguments and local variables is allocated, address of the actual arguments are substituted in the corresponding formal arguments, and the statements in the function body are executed.

Passing Arguments to a Function Call By Address - Important The following points must be noted about passing arguments using call by value mechanism: As soon as the called function finishes its execution, the memory allocated for it is de-allocated, i.e., the values of formal arguments and local variable are destroyed, and finally the control is transferred back to the calling function. Any change made to the formal arguments will have immediate effect on actual arguments, since the function will be working on actual arguments through pointers.

Call By Value Call By Address - DIFFERENCE Call by Value The actual arguments can be constants, variables, or expressions. The values of actual arguments are substituted in formal arguments which are ordinary variables. Any change made to the formal arguments will have no effect on actual arguments, since the function will only be using the local copy of the arguments. Call by Address The actual arguments can only be variables. The address of the actual arguments are substituted in formal arguments which are pointer variable. Any change made to the formal arguments will have immediate effect on actual arguments, since the function will be working on actual arguments through pointers.

Practice Questions Predict the result of the following code segment: void test(int *address) { printf( %u : %u : %u, address, &address, *address); } void main(void) { int value = 50; test(&value); }

Practice Questions Predict the result of the following code segment: void main(void) { int value = 50; int *address = &value; printf( %d : %d : %d, address, &address, *address); } Also state why the output of this code differs from that of previous problem.

Fill in the blank with suitable expression that performs the task equivalent to the statement: int *address = &value; int *address; = &value ; Practice Questions Fill in the blanks in the context of statement: int *address = &value; The expressions and *address will return same value. The expressions &address will return address of. The expression address and will return same value/address.

Practice Questions What is wrong with the following code segment? void testptrs(int iptr, float *fptr, char *cptr) { printf( The address of i is: %u, iptr); printf( The value of f is: %f, fptr); printf( The address of c is: %u, cptr); } void main() { int i = 100; float f = 35.79, char c = $ ; testprts(&i, &f, &c); }

Practice Questions Write the problem given in LECTURE 13 using pointers rather than returning values from functions.