Pointers. Pointers. Pointers (cont) CS 217

Similar documents
Pointers. Pointer References

... Lecture 12. Pointers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Pointers and Arrays. More Pointer Examples. Pointers CS 217

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

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

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

Array Initialization

Arrays and Pointers (part 1)

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

Summer May 18, 2010

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers (part 1)

Course organization. Course introduction ( Week 1)

Algorithms & Data Structures

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

SYSC 2006 C Winter 2012

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

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

Homework #3 CS2255 Fall 2012

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

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

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

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

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

MYcsvtu Notes LECTURE 34. POINTERS

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

CS201- Introduction to Programming Current Quizzes

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

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?

Introduction to Computer Science Midterm 3 Fall, Points

Lecture 04 Introduction to pointers

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

Dynamic arrays / C Strings

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):

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

EM108 Software Development for Engineers

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

What is an algorithm?

Chapter 7 C Pointers

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Dynamic memory allocation (malloc)

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.

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

C++ for Java Programmers

This page intentionally left blank

C++ Programming Chapter 7 Pointers

Pointers. 10/5/07 Pointers 1

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

Arrays, Pointers and Memory Management

Pointers, Dynamic Data, and Reference Types

Dynamic memory allocation

Fundamental of Programming (C)

ME964 High Performance Computing for Engineering Applications

CSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays

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.

CSE2301. Arrays. Arrays. Arrays and Pointers

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

Multiple Choice Questions ( 1 mark)

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

[0569] p 0318 garbage

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

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Understanding Pointers

POINTER & REFERENCE VARIABLES

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Procedural programming with C

Chapter 13. Functions and Parameter Passing (Part 2)

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

CS 223: Data Structures and Programming Techniques. Exam 2


CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

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

FUNCTIONS POINTERS. Pointers. Functions

Systems Programming and Computer Architecture ( )

Intermediate Programming, Spring 2017*

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

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

CS 0449 Sample Midterm

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

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

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

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

1 Lexical Considerations

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

C-LANGUAGE CURRICULAM

Object oriented programming C++

Goals of this Lecture

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

Problem Solving and 'C' Programming

Contents. A Review of C language. Visual C Visual C++ 6.0

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

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

CS 241 Data Organization Pointers and Arrays

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Transcription:

Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char, so p is a pointer to a char Pointers (cont) Suppose x and y are integers and p is a pointer to an integer int x, y int *p; p = &x; p gets the address of x y = *p; y gets the value pointed to by p y = *(&x); same as y = x 5 x p 5 y 1

Pointers (cont) Pointers (e.g., *p) are variables int x, y; int *px, *py; px = &x; px is the address of x *px = 0; sets x to 0 py = px; py also points to x *py += 1; increments x to 1 y = (*px)++; sets y to 1, x to 2 Pointer Arithmetic Pointer arithmetic takes into account the stride (size of) the value pointed to char *p; p += i; increments p by i elements p -= i; decrements p by i elements p++; increments p by 1 element p--; decrements p by 1 element If p and q are pointers to same type p q number of elements between p and q Other ops: p < q; <= ==!= >= > p and q must point to the same array no runtime checks to ensure this Pointers & Arrays Array names are constant pointers int a[10]; int *p; p = a; p points to a[0] a++; illegal; can t change a constant p++; legal; p is a variable Subscripting is defined in terms of pointers a[i] *(a+i) &a[i] a+i p = &a[0] &*(a+0) &*a a 2

Pointers & Arrays Pointers can walk along arrays int a[10], i, *p, x; p = a; p gets &a[0] x = *p; x gets a[0] p = p + 1; p points to a[1] y = *p; x gets a[1] p++; p points to a[2]; Pointers & Strings String constants denote constant ptrs to actual chars char *msg = HELLO ; and char msg[] = HELLO ; char *p = msg; p points to 1 st character of HELLO Strings can be used whenever arrays of chars are used static char digits[] = 0123456789 ; putchar(digits[i]); H E L L O 0 msg Argument Passing Passing pointers to functions simulates passing arguments by reference void swap(int x, int y) { int t; void swap(int *x, int *y) { int t; t = x; x = y; y = t; t = *x; *x = *y; *y = t; int a = 1, b = 2; swap(a, b); printf( %d %d\n,a,b); Passing by value int a = 1, b = 2; swap(&a, &b); printf( %d %d\n,a,b); Passing by reference 3

Pointer & Array Parameters Array parameters: formals are not constant; they are variables passing an array passes a pointer to 1 st element arrays (and only arrays) are passed by reference void f(t a[]) { is equivalent to void f(t *a) { Example Copying strings void scopy(char *s, char *t) copies t to s Array version void scopy(char s[], char t[]) { int i; for (i = 0; t[i]; i++) s[i] = t[i]; Pointer version void scopy(char *s, char *t) { while (*t) *s++ = *t++; Arrays of Pointers Used to build tabular structures Indirection (*) has lower precedence than [] char *line[100]; same as char *(line[100]); declares array of pointers to strings *line[i] refers to the 0 th character of the ith string 4

Arrays of Pointers (cont) Can be initialized Example char *name[] = { January, February,, December ; Another example int a, b; int *x[] = {&a, &b, &b, &a, NULL; Arrays of Pointers (cont) Similar to multi-dimensional arrays int a[10][10]; both a[i][j] int *b[10]; b[i][j] are legal references to ints Array a: 2-dimensional 10x10 array storage for 100 elements allocated at compile time a[6] is a constant; a[i] cannot change at runtime each row of a has 10 elements Array b: an array of 10 pointers; each element could point to an array storage for 10 pointers allocated at compile time values of these pointers must be initialized at runtime b[6] is a variable; b[i] can change at runtime each row of b can have a different length (ragged array) Array of Pointers (cont) Another example void f(int *a[10]); is the same as void f(int **a); and void g(int a[][10]); is the same as void g(int (*a)[10]); **a = 1; is legal in both f & g 5

Command-Line Arguments By convention, main is called with 2 arguments int main(int argc, char *argv[]) argc is the number of arguments argv is an array of pointers to the arguments Example: echo hello world argc = 3 argv[0] = echo argv[1] = hello argv[2] = world argv[3] = NULL Implementation of echo int main(int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) printf( %s%c,argv[i], (i < argc-1)? : \n ); return 0; Pointers to Functions Used to parameterize other functions void SortStrings(char *v[], int n, int (*compare)(char *, char *)) { if ((*compare)(v[i],v[j]) <= 0) { 6

Pointers to Functions (cont) Declaration syntax can confuse: int (*compare)(void *, void*) declares compare to be a pointer to a function that takes two void * arguments and returns an int int *compare(void *, void *) declares compare to be a function that takes two void * arguments and returns a pointer to an int Pointers to Functions (cont) Invocation syntax can also confuse: (*compare)(v[i], v[j]) calls the function pointed to by compare with the arguments v[i] and v[j] *compare(v[i], v[j]) calls the function compare with arguments v[i] and v[j], then dereferences the value returned Function call has higher precedence than dereferencing Pointers to Functions (cont) A function name itself is a constant pointer to a function (like an array name) extern int strcmp(char *, char *); main(int argc, char *argv[]) { char *v[vsize]; sort(v, VSIZE, strcmp); 7

Pointers to Functions (cont) Arrays of pointers to functions extern int mul(int, int); extern int add(int, int); int (*operators[])(int, int) = { mul, add, ; To invoke (*operators[i])(a, b); 8