Example: Pointer Basics

Similar documents
POINTER & REFERENCE VARIABLES

Variation of Pointers

Pointers. 10/5/07 Pointers 1

Tutorial 10 Pointers in C. Shuyue Hu

Programming Language B

PROGRAMMAZIONE I A.A. 2017/2018

[0569] p 0318 garbage

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

Principles of C and Memory Management

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

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

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

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

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

Basic and Practice in Programming Lab7

Fundamental of Programming (C)

Pointers and Arrays 1

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

CS 108 Computing Fundamentals. October/November Array Bootcamp

Introduction to Scientific Computing and Problem Solving

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

Arrays and Pointers (part 2) Be extra careful with pointers!

for Loop Lesson 3 Outline

... Lecture 12. Pointers

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

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

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

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

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.

Arrays and Pointers. Lecture Plan.

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Homework #3 CS2255 Fall 2012

Introduction to C. Systems Programming Concepts

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Character Strings. String-copy Example

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

Chapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture4 Pointers

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

CS 161 Exam II Winter 2018 FORM 1

More about BOOLEAN issues

Today s class. Review of more C Operating system overview. Informationsteknologi

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

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

Dynamic Allocation of Memory Space

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

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

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

EM108 Software Development for Engineers

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Intermediate Programming, Spring 2017*

Arrays and Pointers. CSE 2031 Fall November 11, 2013

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Programming with Indexed Variables

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

C Programming Language

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Computer Programming Lecture 12 Pointers

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

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Programming for Engineers Pointers

Slides adopted from T. Ferguson Spring 2016

Procedural programming with C

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?

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Pointers. Pointers. Pointers (cont) CS 217

Arrays and Pointers in C. Alan L. Cox

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

Exam 3 Chapters 7 & 9

Procedural Programming & Fundamentals of Programming

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

Memory, Data, & Addressing II CSE 351 Spring

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

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

Object oriented programming C++

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

#1 (Declarations) Declare the following variables. Do not initialize them.

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Pointers. Pointer References

ESC101N Fundamentals of Computing

Programming for Electrical and Computer Engineers. Pointers and Arrays

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

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

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

ECE 15B COMPUTER ORGANIZATION

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

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

More Pointers Week 11

Transcription:

Example: Pointer Basics #include <stdio.h> int (void) { float a1, a2; /* Simple variables */ float *p1, *p2, *w; /* Pointers to variables of type float */ a1 = 10.0, a2 = 20.0; printf("after a1 = 10.0; a2 = 20.0\n"); printf(" a1=%f &a1=%ld\n", a1, &a1); printf(" a2=%f &a2=%ld\n\n", a2, &a2); p1 = &a1; p2 = &a2; printf("after p1 = &a1; p2 = &a2\n"); printf(" *p1=%f p1=%ld &p1=%ld\n", *p1, p1, &p1); printf(" *p2=%f p2=%ld &p2=%ld\n\n", *p2, p2, &p2); w = p1; printf("after w = p1\n"); printf(" *w=%f w=%ld &w=%ld\n\n", *w, w, &w); *p1 = 99.0; printf("after *p1 = 99.0\n"); printf(" *p1=%f p1=%ld &p1=%ld\n", *p1, p1, &p1); printf(" a1=%f &a1=%ld\n\n", a1, &a1); a1 = *p2 + 7; printf("after a1 = *p2 + 7\n"); printf(" a1=%f &a1=%ld\n\n", a1, &a1); *p2 = *p1 * 2; printf("after *p2 = *p1 * 2\n"); printf(" *p2=%f p2=%ld &p2=%ld\n", *p2, p2, &p2); printf(" a2=%f &a2=%ld\n\n", a2, &a2); } return 0; Actual output: After a1 = 10.0; a2 = 20.0 a1=10.000000 &a1=1012 a2=20.000000 &a2=1016 After p1 = &a1; p2 = &a2 *p1=10.000000 p1=1012 &p1=1000 *p2=20.000000 p2=1016 &p2=1004 After w = p1 *w=10.000000 w=1012 &w=1008 After *p1 = 99.0 *p1=99.000000 p1=1012 &p1=1000 a1=99.000000 &a1=1012 After a1 = *p2 + 7 a1=27.000000 &a1=1012 After *p2 = *p1 * 2 *p2=54.000000 p2=1016 &p2=1004 a2=54.000000 &a2=1016 1

Assumptions: The system allocates memory space to store the requested variables at the addresses shown below. The pointer variables are not initialized. System State: After declaring local float variables a1 and a2 and pointer variables p1, p2, and w in function () p1 1000? p2 1004? a1 1012? a2 1016? w 1008? System State: After executing a1 = 10.0; a2 = 20.0; p1 1000? p2 1004? a1 1012 10.0 w 1008? System State: After executing p1 = &a1; p2 = &a2 a1 1012 10.0 w 1008? 2

System State: After executing w = p1; a1 1012 10.0 w 1008 1012 System State: After executing *p1 = 99.0; a1 1012 99.0 w 1008 1012 System State: After executing a1 = *p2 + 7; a1 1012 27.0 w 1008 1012 3

System State: After executing *p2 = *p1 * 2; (Note: * is used here both for dereferencing and for multiplication.) a1 1012 27.0 a2 1016 54.0 w 1008 1012 4

Levels of Indirection (Pointers that reference other pointers) int x; int * p; int ** q; x = 5; p = &x; q = &p; printf("%d %d %d\n", x, *p, **q); /* Displays 5 5 5 */ *p = 4; printf("%d %d %d\n", x, *p, **q); /* Displays 4 4 4 */ **q = 3; printf("%d %d %d\n", x, *p, **q); /* Displays 3 3 3 */ p = NULL; 5

System State: After declaring integer variable x and pointer variables p and q. x 5, &p 1300, p?, *p?, &q 1500, q?, *q?, **q? q 1500? p 1300? x 1150 5 System State: After executing p = &x; x 5, &p 1300, p 1150, *p 5, &q 1500, q?, *q?, **q? q 1500? p 1300 1150 x 1150 5 System State: After executing q = &p; x 5, &p 1300, p 1150, *p 5, &q 1500, q 1300, *q 1150, **q 5 q 1500 1300 p 1300 1150 x 1150 5 System State: After executing *p = 4; x 4, &p 1300, p 1150, *p 4, &q 1500, q 1300, *q 1150, **q 4 q 1500 1300 p 1300 1150 x 1150 4 System State: After executing **q = 3; x 3, &p 1300, p 1150, *p 3, &q 1500, q 1300, *q 1150, **q 3 q 1500 1300 p 1300 1150 x 1150 3 System State: After executing p = NULL; x 3, &p 1300, p NULL, *p? 1, &q 1500, q 1300, *q NULL, **q? 1 q 1500 1300 p 1300 NULL x 1150 3 1 Trying to dereference a NULL pointer will cause a memory fault error during program execution. 6

Pointer Arithmetic #include <stdio.h> #define SIZE 3 int (void) { double x[size] = {-1.0, 5.0, 9.8}; double * p; int i; printf("output x[i]\n"); for (i = 0; i < SIZE; i++) printf(" x=%ld, i=%d: %6.2f\n", x, i, x[i]); p = x; printf("\noutput p[i]\n"); for (i = 0; i < SIZE; i++) printf(" p=%ld, i=%d: %6.2f\n", p, i, p[i]); printf("\noutput *(p+i) loop\n"); for (i = 0; i < SIZE; i++) printf(" p+i=%ld: %6.2f\n", p+i, *(p+i)); printf("\noutput *p++ loop\n"); for (i = 0; i < SIZE; i++) printf(" p=%ld: %6.2f\n", p, *p++); } printf("\n"); return 0; Actual output: Output x[i] x=2000, i=0: -1.00 x=2000, i=1: 5.00 x=2000, i=2: 9.80 Output p[i] p=2000, i=0: -1.00 p=2000, i=1: 5.00 p=2000, i=2: 9.80 Output *(p+i) loop p+i=2000: -1.00 p+i=2008: 5.00 p+i=2016: 9.80 Output *p++ loop p=2000: -1.00 p=2008: 5.00 p=2016: 9.80 After the *p++ loop is done, the value of p has changed and is no longer equal to x. 7

What happens when a pointer variable is incremented using p++? Notes: *(p+0) *(p-0) *(p) *p Name Contents x 2000 p 1000 2000 Address Equivalent access methods Contents 2000 x[0] p[0] *(p+0) -1.0 2008 x[1] p[1] *(p+1) 5.0 2016 x[2] p[2] *(p+2) 9.8 2024? Name Contents x 2000 p 1000 2008 Address Equivalent access methods Contents 2000 x[0] *(p-1) -1.0 2008 x[1] p[0] *(p+0) 5.0 2016 x[2] p[1] *(p+1) 9.8 2024? Name Contents x 2000 p 1000 2016 Address Equivalent access methods Contents 2000 x[0] *(p-2) -1.0 2008 x[1] *(p-1) 5.0 2016 x[2] p[0] *(p+0) 9.8 2024? 8