Pointers. 10/5/07 Pointers 1

Similar documents
by Pearson Education, Inc. All Rights Reserved.

[0569] p 0318 garbage

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

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

What is an algorithm?

SYSC 2006 C Winter 2012

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

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

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

BITG 1113: POINTER LECTURE 12

Fundamentals of Programming Session 20

PROGRAMMAZIONE I A.A. 2017/2018

Intermediate Programming, Spring 2017*

More about BOOLEAN issues

Data Types and Variables in C language

Example: Pointer Basics

MYcsvtu Notes LECTURE 34. POINTERS

C++ Programming Chapter 7 Pointers

Programming in C - Part 2

Variation of Pointers

DECLARAING AND INITIALIZING POINTERS

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

EM108 Software Development for Engineers

Homework #3 CS2255 Fall 2012

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

Procedural programming with C

Pointers as Arguments

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

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

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

Pointers! Arizona State University 1

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

C++ for Java Programmers

Lecture 2: C Programm

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

POINTER & REFERENCE VARIABLES

Pointers in C/C++ 1 Memory Addresses 2

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Lecture 9 - Pointers 1

Lecture 05 POINTERS 1

Assist. Prof. Dr. Caner ÖZCAN

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

... Lecture 12. Pointers

Differentiate Between Keywords and Identifiers

Programming for Engineers Pointers

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Goals of this Lecture

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Programming in C++ 5. Integral data types

Tutorial 10 Pointers in C. Shuyue Hu

Fundamentals of Programming Session 20

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

Computer Programming. Pointers. Marius Minea. 20 November 2017

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

CSC 211 Intermediate Programming. Pointers

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

A Fast Review of C Essentials Part I

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

Introduction to Scientific Computing and Problem Solving

Chapter 6 - Pointers

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

CS 61c: Great Ideas in Computer Architecture

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

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

Solution Manual for Data Structures and Problem Solving Using C++ 2nd edition by Mark A. Weiss

A flow chart is a graphical or symbolic representation of a process.

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointers. Pointers. Pointers (cont) CS 217

Data Representation and Storage

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Programming for Engineers: Operators, Expressions, and Statem

C-LANGUAGE CURRICULAM

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

Laboratory 3 OPERATORS, EXPRESSIONS, INSTRUCTIONS I. THEORETICAL BACKGROUND

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

Basic Assignment and Arithmetic Operators

C Pointers. 7.2 Pointer Variable Definitions and Initialization

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

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

Computers Programming Course 5. Iulian Năstac

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Computers Programming Course 6. Iulian Năstac

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

CSCI 171 Chapter Outlines

Lecture 23: Pointer Arithmetic

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Understanding Pointers

Fundamental of Programming (C)

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

Operators & Expressions

CPSC 427a: Object-Oriented Programming

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

Transcription:

Pointers 10/5/07 Pointers 1

10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it.

10/5/07 Pointers 3 Variable For example 1:#include <stdio.h> 2:int main() 3:{ 4: float fl=3.14; 5: printf("%.2f\n", fl); 6: return 0; 7:}

Variable At line (4) in the program above, the computer reserves memory for fl. In our examples, we'll assume that a float requires 4 bytes. Depending on the computer's architecture, a float may require 2, 4, 8 or some other number of bytes. When fl is used in line (5), two distinct steps occur: o o The program finds and grabs the address reserved for fl. In this example 924. The contents stored at that address are retrieved. To generalize, whenever any variable is accessed, the above two distinct steps occur to retrieve the contents of the variable. 10/5/07 Pointers 4

10/5/07 Pointers 5 More examples 1: #include <stdio.h> 2: int main() 3: { 4: float fl=3.14; 5: printf("fl's address=%u\n", (unsigned int) &fl); 6: return 0; 7: } This Code prints out the address of fl variable.

10/5/07 Pointers 6 Pointers Pointers are one of the most powerful features of C Pointers give programmers more control over the computer s memory A pointer is the memory address of a variable How are pointer variables declared in C?

10/5/07 Pointers 7 Pointer Declarations The memory address of a variable can be stored in another variable called a pointer Pointers are declared using the * operator The following declares a pointer to an integer o int *px; In the following statement, x is an integer and px is a pointer to an integer o int *px, x;

10/5/07 Pointers 8 Address Operator How do we assign to a pointer the address of a variable? Use the address operator (&) & returns the memory address of it s operand Example: o px = &x; Where have we used & before?

10/5/07 Pointers 9 Address Operator Operand of the address operator must be an lvalue An lvalue is something to which a value can be assigned Address operator cannot be applied to constants o int const NUM = 98; o px = &NUM; // ERROR o px = &8; // ERROR

10/5/07 Pointers 10 Pointer Operations int x, *px; x = 8; // set x to a value of 8 px = &x; // set the pointer variable to point // to the address of x printf( x is : %d \n,x); printf("address of x is: %u,px ); printf("address of x is: %u,& x);

10/5/07 Pointers 11 Indirection Operator How can we use the pointer variable to modify the value in the variable? o i.e. how to use px to change the value of x Answer: use the indirection operator (*) The * operator returns a synonym to whatever the pointer variable is pointing to Using the example on the previous slide o printf("px is pointing to: %d \n,*px);

10/5/07 Pointers 12 Indirection Operator Using * as we did in the previous example is called dereferencing the pointer Using our example, how can we dereference px so that it changes the value of x from 8 to 10? How can we change the value of x to a value entered by the user using the indirection operator?

10/5/07 Pointers 13 Common Pointer Mistakes What is wrong with the following? int x, *px; x = 8; *px = 2; px = 9; *x = 4;

10/5/07 Pointers 14 Pointers and Functions Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there. We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap. This is known as "call by value".

10/5/07 Pointers 15 Pointers and Functions If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables. Lets look at couple of example.

10/5/07 Pointers 16 Passing by reference void square3 (int *pnum) { *pnum *= *pnum; } What would a function call to the above function look like?

10/5/07 Pointers 17 Function Call intval = 5; square3( &intval ); printf( %d,intval);

10/5/07 Pointers 18 Arithmetic and Logical Operations on Pointers A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer can used to perform arithmetic operation on the value its pointing to. Pointer variables can be used in comparisons, but usually only in a comparison to NULL.

Arithmetic Operations on Pointers 10/5/07 Pointers 19 When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to. For example, if the pointer valptr contains the address of a double precision variable and that address is 234567870, then the statement: valptr = valptr + 2; would change valptr to 234567886

Arithmetic Operations on Pointers 10/5/07 Pointers 20 If you want use a pointer to perform arithmetic operations on a value it pointing then you have to deference it. int x, * ptrx; x = 8; ptrx = &x; ptrx = *ptrx + 1; (value of x=9) ptrx = *ptrx * 2; (value of x=18)

10/5/07 Pointers 21 Precedence Higher (assiociate left to right): (), [], {} Lower (right to left): ++, --, *, & Even lower (left to right): +, -