... Lecture 12. Pointers

Similar documents
Pointers. Pointer References

Pointers. Pointers. Pointers (cont) CS 217

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

SYSC 2006 C Winter 2012

MYcsvtu Notes LECTURE 34. POINTERS

Programming for Engineers Pointers

Arrays and Pointers (part 1)

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Lecture 14. Dynamic Memory Allocation

Fundamentals of Programming Session 20

Fundamentals of Programming Session 20

Lecture 3. More About C

Arrays and Pointers (part 1)

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

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

Fundamental of Programming (C)

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

POINTER & REFERENCE VARIABLES

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

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Pointers as Arguments

Procedural programming with C

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

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

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

Decimal Representation

Pointers. 10/5/07 Pointers 1

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

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

PROGRAMMAZIONE I A.A. 2017/2018

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

Chapter 6 - Pointers

Array Initialization

Week 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Operators & Expressions

Programming in C++ 6. Floating point data types

Fundamentals of Programming Session 19

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

Lecture 9 - Pointers 1

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

Operators and Expressions:

Computer Programming Lecture 12 Pointers

ECE 15B COMPUTER ORGANIZATION

Character Strings. String-copy Example

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

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

C++ Programming Chapter 7 Pointers

Lecture 05 POINTERS 1

Arrays and Pointers. Lecture Plan.

Relationship between Pointers and Arrays

A Shotgun Introduction to C

C++ for Java Programmers

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

Fundamental of Programming (C)

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

EM108 Software Development for Engineers

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

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

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

Multidimension array, array of strings

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

Example: Pointer Basics

Principles of C and Memory Management

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

Goals of this Lecture

Arrays, Pointers and Memory Management

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Lecture 04 Introduction to pointers

A Shotgun Introduction to C

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

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

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Tutorial 10 Pointers in C. Shuyue Hu

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

by Pearson Education, Inc. All Rights Reserved.

ESC101N Fundamentals of Computing

C Pointers Pearson Education, Inc. All rights reserved.

Fundamentals of Programming

CS 61c: Great Ideas in Computer Architecture

CSE2301. Arrays. Arrays. Arrays and Pointers

Chapter-8 DATA TYPES. Introduction. Variable:

Programming for Engineers: Operators, Expressions, and Statem

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Introduction to Scientific Computing and Problem Solving

DECLARAING AND INITIALIZING POINTERS

Programming in C++ 5. Integral data types

Variation of Pointers

Operators And Expressions

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

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

Chapter 7 C Pointers

Summer May 18, 2010

BASIC ELEMENTS OF A COMPUTER PROGRAM

Program construction in C++ for Scientific Computing

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

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

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

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

Transcription:

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-1 Lecture 12. Pointers Variables denote locations in memory that can hold values; arrays denote contiguous locations int i = 8, sum = -456; float average = 34.5; unsigned count[4]; The location of a variable is its lvalue or address; the contents stored in that location is its rvalue A pointer is a variable whose rvalue is the lvalue of another variable the address of that variable Pointers are typed: a pointer to an int may hold only the lvalue of an int variable If p points to sum, q points to count[2]: int *p; unsigned *q; p = q = &count[2]; p and q cannot point to average The null pointer denoted NULL points to nothing p = NULL; 09A8 16 09AC 16 09B0 16 09B4 16 0F10 16 0F14 16 0F18 16 0F1C 16 8 i -456 sum 34.5... 13A4 16 09AC p 13A8 16 0F18 q average count[0] count[1] count[2] count[3]

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-2 Pointer Operations Two fundamental operations: creating pointers, accessing the values they point to unary & address of returns the address of its lvalue operand as an rvalue unary * indirection returns the lvalue given by its pointer operand s rvalue Suppose x and y are ints, p is a pointer to an int p = &x; p is assigned the address of x 5 x y = *p; y is the value pointed to by p p y = *(&x); same as y = x Declaration syntax for pointer types mimics the use of pointer variables in expressions int x, y; int *p; *p is an int, so p must be a pointer to an int 5 y Unary * and & have higher precedence than most other operators y = *p + 1; y = (*p) + 1; y = *p++; y = *(p++);

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-3 Indirection Pointer indirection (e.g., *p) yields an lvalue a variable and pointer values can be manipulated like other values int x, y, *px, *py; px = &x; px is the address of x no effect on x *px = 0; sets x to 0 no effect on px py = px; py also points to x no effect on px or x *py += 1; increments x to 1 no effect on px or py y = (*px)++; sets y to 1, x to 2 no effect on px or py Passing pointer arguments simulates passing arguments by reference void swap(int x, int y) { int t; t = x; x = y; y = t; int a = 1, b = 2; swap(a, b); printf("%d %d\n", a, b); 1 2 void swap(int *x, int *y) { int t; t = *x; *x = *y; *y = t; int a = 1, b = 2; swap(&a, &b); printf("%d %d\n", a, b); 2 1

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-4 Pointers and Arrays Pointers can walk along arrays by pointing to each element in turn int a[10], i, *p, x; p = &a[0]; x = *p; x = *(p + 1); p = p + 1; p++; p is assigned the address of the 1st element of a x is assigned a[0] x is assigned a[1] p is assigned the address of a[1], by definition p points to a[2] Pointer arithmetic: If p points to a[i], p + k points to a[i+k] An array name is a constant pointer to the first element of the array p = a; p is assigned the address of a[0] a++; illegal: can t change a constant p++; legal: p is a variable The idiom *p++ walks along the array pointed to by p p = a; for (i = 0; i < 10; i++) printf("%d\n", *p++); for (i = 0; i < 10; i++) printf("%d\n", a[i]); Both loops print the same output, both are efficient, both are acceptable

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-5 Pointers and Array Parameters An array parameter type is identical to a pointer to the element type Array parameters are not constants, they are variables Passing an array as an actual argument passes a pointer to the first element In effect, arrays and only arrays are passed by-reference void print(int x[], int size) { void print(int *x, int size) { int i; for (i = 0; i < size; i++) while (size-- > 0) printf("%d\n", x[i]); printf("%d\n", *x++); A string is an array of characters; the name of a character array is thus a char * String functions can be written using arrays or pointers, but often return pointers char *strcpy(char *dst, char *src) copies src to dst, then returns dst char *strcpy(char dst[], char src[]) { int i; for (i = 0; src[i]!= \0 ; i++) dst[i] = src[i]; dst[i] = \0 ; return dst;

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-6 Pointer version Pointers and Array Parameters, cont d char *strcpy(char *dst, char *src) { char *d = dst, *s = src; while (*d = *s) { while ((*d = *s)!= \0 ) d++; s++; return dst; Idiomatic version char *strcpy(char *dst, char *src) { char *d = dst; while (*dst++ = *src++) while ((*dst++ = *src++)!= \0 ) ; return d; Pointer versions might be faster, but strive for clarity, not microefficiency

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-7 Arrays of Pointers Arrays of pointers help build tabular structures char *suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" ; char *faces[] = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" ; Declare suits and faces each to be an array of pointers to characters, not a pointer to an array of characters, and initialize them as shown Indirection (*) has lower precedence than [] char *suits[]; is the same as char *(suits[]); Declaration mimics use: *suits[i] refers to the 0th character in the ith string printsuit(int card) { printf("%c", *suits[card%13]); A string constant is shorthand for the name of an array of characters print("0123456789abcdef"[n%b]); suits H e a r t s \0 D i a m o n d s \0 C l u b s \0 S p a d e s \0 char digits[] = "0123456789ABCDEF"; print(digits[n%b]);

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-8 Common Errors Only addresses can be assigned to pointers int *p, i; p = i; p = &i; Only addresses of variables of the correct types can be assigned to pointers int *p; float *p; float x; p = &x; Only pointers can be used with indirection p = *i; i = *p;? Pointers must be initialized to valid addresses before using indirection *p = 5; printf("%d\n", *p); p = &i; The null pointer must not be dereferenced, because it points to nothing p = NULL; *p = 6; p = &i;

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-9 Common Errors, cont d Pointers must point to variables that exist! See page 4-8 int *SumPtr(int a, int b) { int sum = a + b; return p = SumPtr(2, 5); printf("%d\n", *p); sum does not exist! char *itoa(int n) { char buf[100]; sprintf(buf, "%d", n); return buf; char *s; s = itoa(56); printf("%s\n", s); buf does not exist! sprintf is like printf, but stores the output in a string When faced with bugs involving a pointer, ask: Is this pointer initialized? Does the memory it points to exist?