Programming for Engineers Pointers

Similar documents
Fundamentals of Programming Session 20

Chapter 6 - Pointers

Lecture 05 POINTERS 1

C++ Programming Chapter 7 Pointers

by Pearson Education, Inc. All Rights Reserved.

Pointers and Strings Prentice Hall, Inc. All rights reserved.

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

Relationship between Pointers and Arrays

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

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

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

C Pointers Pearson Education, Inc. All rights reserved.

MYcsvtu Notes LECTURE 34. POINTERS

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

OBJECT ORIENTED PROGRAMMING USING C++

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

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

CSC 211 Intermediate Programming. Arrays & Pointers

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

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

Lecture 9 - Pointers 1

... Lecture 12. Pointers

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Functions. Introduction :

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

Chapter 8 STRUCTURES IN C

Lecture 4. Default Arguments. Set defaults in function prototype. int myfunction( int x = 1, int y = 2, int z = 3 );

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

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

What is an algorithm?

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

Tutorial 10 Pointers in C. Shuyue Hu

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

CSC 211 Intermediate Programming. Pointers

Variation of Pointers

Chapter 7 C Pointers

Assist. Prof. Dr. Caner ÖZCAN

Content. In this chapter, you will learn:

Fundamental of Programming (C)

Fundamentals of Programming Session 20

Review Questions II KEY

C Programming for Engineers Functions

Pointers. 10/5/07 Pointers 1

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

PROGRAMMAZIONE I A.A. 2017/2018

Pointers and Strings Prentice Hall, Inc. All rights reserved.

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

C Pointers. 7.2 Pointer Variable Definitions and Initialization

More Pointers Week 11

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Programming for Engineers Arrays

Programming for Engineers Introduction to C

Goals of this Lecture

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 4 Certificate in IT SOFTWARE DEVELOPMENT

Pointers and Strings. Adhi Harmoko S, M.Komp

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

Programming for Engineers Functions

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

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

Programming for Engineers Iteration

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

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

IS 0020 Program Design and Software Tools

Differentiate Between Keywords and Identifiers

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

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

Computers Programming Course 6. Iulian Năstac

Functions. Systems Programming Concepts

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

CS302 - Data Structures using C++

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

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

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Functions BCA-105. Few Facts About Functions:

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Infix to Postfix Conversion

today cs3157-fall2002-sklar-lect05 1

Program Organization

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Introduction to Computer Science Midterm 3 Fall, Points

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

Introduction to C++ Introduction to C++ 1

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

CSCI 171 Chapter Outlines

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

Lecture Data Structure Stack

Program Organization

SYSC 2006 C Winter 2012

ECE 30 Introduction to Computer Engineering

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

C++ for Java Programmers

More about BOOLEAN issues

Lecture 09X: C Func/on Pointers Concurrent and Mul/core Programming

Technical Questions. Q 1) What are the key features in C programming language?

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Transcription:

Programming for Engineers Pointers ICEN 200 Spring 2018 Prof. Dola Saha 1

Pointers Pointers are variables whose values are memory addresses. A variable name directly references a value, and a pointer indirectly references a value. Referencing a value through a pointer is called indirection. 2

Declaring Pointers o Pointers must be defined before they can be used. The definition int *countptr, count; specifies that variable countptr is of type int * (i.e., a pointer to an integer). The variable count is defined to be an int, not a pointer to an int. 3

Initializing Pointers Pointers should be initialized when they re defined or they can be assigned a value. A pointer may be initialized to NULL, 0 or an address. A pointer with the value NULL points to nothing. NULL is a symbolic constant defined in the <stddef.h> header (and several other headers, such as <stdio.h>). Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred. When 0 is assigned, it s first converted to a pointer of the appropriate type. The value 0 is the only integer value that can be assigned directly to a pointer variable. 4

Pointer Operator The &, or address operator, is a unary operator that returns the address of its operand. Example definition o int y = 5; int *yptr; the statement o yptr = &y; assigns the address of the variable y to pointer variable yptr. Variable yptr is then said to point to y. Graphical Representation Memory Representation 5

Indirection (*) Operator The unary * operator, commonly referred to as the indirection operator or dereferencing operator, returns the value of the object to which its operand (i.e., a pointer) points. Example: o printf("%d", *yptr); prints the value of variable that yptr is pointing to In this case it is y, whose value is 5. Using * in this manner is called dereferencing a pointer. 6

Using & and * 7

Pass by value 8

Pass by reference simulating with Pointer 9

Pass by value (1) 10

Pass by value (2) 11

Pass by value (3) 12

Pass by reference (1) 13

Pass by reference (2) 14

Determine Size of Data Types (1) 15

Determine Size of Data Types (2) 16

Pointer Arithmetic A pointer may be incremented (++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=) one pointer may be subtracted from another this last operation is meaningful only when both pointers point to elements of the same array. When an integer n is added to or subtracted from a pointer Pointer is incremented or decremented by that integer times the size of the object to which the pointer refers. vptr+=2; 17

Pointer and Array Arrays and pointers are intimately related in C and often may be used interchangeably. An array name can be thought of as a constant pointer. Pointers can be used to do any operation involving array indexing. Set bptr equal to the address of the first element in array b with the statement bptr = b; Address of the array s first element: bptr = &b[0]; 18

Pointer and Array Array element b[3] with pointer expression *(bptr + 3) The 3 in the expression is the offset to the pointer. This notation is referred to as pointer/offset notation. Address of b[3] can be referenced as &b[3] (bptr+3) 19

Access array elements by pointer (1) 20

Access array elements by pointer (2) 21

Pointer Notation with Arrays (1) 22

Pointer Notation with Arrays (2) 23

Pointer Notation with Arrays (3) 24

Array of Pointers Arrays may contain pointers const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; suit[0] suit[1] suit[2] 'H' 'e' 'a' 'r' 't' 's' '\0' 'D' 'i' 'a' 'm' 'o' 'n' 'd' 's' '\0' 'C' 'l' 'u' 'b' 's' '\0' suit[3] 'S' 'p' 'a' 'd' 'e' 's' '\0' Fig. 7.22 Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King 0 1 2 3 4 5 6 7 8 9 10 11 12 Hearts 0 Diamonds 1 Clubs 2 Spades 3 deck[2][12] represents the King of Clubs Clubs King 25

Pointers to Functions A pointer to a function contains address of function in the memory. // prototypes void function1( int a ); void function2( int b ); void function3( int c ); // initialize array of 3 pointers to functions that each take an // int argument and return void void (*f[ 3 ])( int ) = { function1, function2, function3 }; // invoke function at location choice in array f and pass // choice as an argument (*f[ choice ])( choice ); 26

Stack - Push and Pop with Pointers 1. void 2. push(char stack[], /* input/output - the stack */ 3. char item, /* input - data being pushed onto the stack */ 4. int *top, /* input/output - pointer to top of stack */ 5. int max_size) /* input - maximum size of stack */ 6. { 7. if (*top < max_size-1) { 8. ++(*top); 9. stack[*top] = item; 10. } 11. } 12. 13. char 14. pop(char stack[], /* input/output - the stack */ 15. int *top) /* input/output - pointer to top of stack */ 16. { 17. char item; /* value popped off the stack */ 18. 19. if (*top >= 0) { 20. item = stack[*top]; 21. --(*top); 22. } else { 23. item = STACK_EMPTY; 24. } 25. 26. return (item); 27. } 27

Calculate Execution Time #include <time.h> clock_t start, end; start = clock(); // Write the code that needs to be timed end = clock(); double time_taken = ((double)(end-start)) / CLOCKS_PER_SEC; printf("the time taken for this program is %lf\n", time_taken); 28