Homework Starting K&R Chapter 5 Good tutorial on pointers

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 10.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Pointers CSE 2451 Rong Shi.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Engineering Computing I Chapter 5 Pointers and Arrays.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Windows Programming Lecture 03. Pointers and Arrays.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Dynamic Allocation in C
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
Pointers What is the data type of pointer variables?
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Chapter 5, Pointers and Arrays
Dynamic Storage Allocation
Pointers and Dynamic Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
Chapter 8 Arrays, Strings and Pointers
UNIT 5 C Pointers.
Pointers and Pointer-Based Strings
Student Book An Introduction
CNG 140 C Programming (Lecture set 10)
Arrays in C.
Pointers.
Arrays and Pointers CSE 2031 Fall September 2018.
Pointers and Arrays Chapter 12
14th September IIT Kanpur
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Programming with Pointers
Pointers.
Beginning C for Engineers
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
Chapter 16 Pointers and Arrays
Chapter 8 Character Arrays and Strings
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Pointers and Pointer-Based Strings
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Homework Continue with K&R Chapter 5 Skipping sections for now
Data Structures and Algorithms Introduction to Pointers
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Chapter 9: Pointers and String
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
CS31 Discussion 1H Fall18: week 6
Presentation transcript:

Homework Starting K&R Chapter 5 Good tutorial on pointers Skipping sections 5.7-5.9 for now Not covering section 5.12 Good tutorial on pointers http://pw1.netcom.com/~tjensen/ptr/pointers.htm

Chapter 5, Pointers and Arrays Declarations of automatic variables: int x = 1, y = 2, z[10]; int *ip; /* ip is a pointer to an int */ ip = &x; /* ip is a pointer to int x */ Read “int *ip” as: "the int pointed to by ip" Trick if it helps: Read “int *ip” right to left Variable ip is a pointer (*) to a variable of type int Show the link on the course website Pointer is an address and the computer allocates 32 bits for this address. It does not relate to the type of the content of that address. So always 4 bytes.

Pointers and Memory Memory Address, Contents, and Variable Names 0xFF1054 x 0xFF1050 y 0xFF104C z[9] … … … 0xFF1028 z[0] 0xFF1024 ip 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x02 0x?? 0x?? 0x?? 0x?? 0x?? 0x?? 0x?? 0x?? 0x00 0xFF 0x10 0x54

Operators (& and *) Operator ‘&’  value of &x is “address of x" ip = &x; /* ip now points to x */ Operator ‘*’  de-references a pointer (indirection) y = *ip; /* set y = x (the int at address ip) */ *ip = 0; /* set x to 0 */ ip = &z[0]; /* set ip to address of z[0] */ *ip = 3; /* set z[0] to 3 */ *ip = *ip + 10; /* set z[0] to 13 */ Note: & and * are unary operators - See pg 53

Pointer Operations More pointer operation examples: *ip + 1; /* add 1 to the int pointed to by ip */ *ip += 1; /* adds one to the int pointed to by ip */ ++*ip; /* pre increments int pointed to by ip */ ++(*ip); /* same as above, binds right to left */ *ip++ /* point to int at pointer ip, post increment ip*/ /* binds right to left as *(ip++) */ (*ip)++; /* post increments int pointed to by ip */ /* need ( ) - otherwise binds as *(ip++) */ same Only *ip++ modifies the ip

Incrementing Pointers A pointer is a number corresponding to the address of the byte used to store the variable When you increment a pointer, the address is incremented by the number of bytes used to store that type of variable For example, a char pointer cp declared as: char *cp; cp++; /* byte address is incremented by 1 */ Char z[10]; Char *ip; Ip = &z[0]; Ip++; // one byte after that -> next element Int z[10]; Int *iq; Iq = &z[0]; Iq++; //four bytes after that -> next element

Incrementing Pointers For example, an int pointer ip declared as: int *ip; ip++; /* byte address is incremented by 4 */ The int pointer is not thought of as being incremented by 4 - that's hidden from the C programmer - it's said to be incremented by the size of the data type that it points at

Pointers as Function Arguments * DOESN'T WORK * * POINTER VERSION * swap (i, j); swap (&i, &j); … … void swap (int a, int b) void swap (int *pa, int *pb) { { int dummy; int dummy; dummy = a; dummy = *pa; a = b; *pa = *pb; b = dummy; *pb = dummy; } }

Declaration and Initialization Example of Declaration and Initialization: int a[10]; int *pa = &a[0]; /* initialize pa to point to a[0] */ When we are initializing in the declaration, the * acts as part of the type for variable pa When we are initializing pa, we are setting pa (not *pa) equal to the address after = sign For normal assignment, we use: pa = &a[0];

Pointers and Arrays C treats an array name (without a subscript value) and a pointer in THE SAME WAY We can write: pa = &a[0]; OR we can write the equivalent: pa = a; Array name "a" acts as specially initialized pointer pointing to element 0 of the array

Pointers and Arrays Given the way incrementing a pointer works, it’s useful for accessing successive elements of an array that it points to: *pa means same as a[0] *(pa + 1) means the same as a[1] *(pa + m) means the same as a[m]

Pointers and Arrays Consider the example: int i, a[ ] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}; int *pa = &a[3]; What is the value of *(pa + 3) ? (12) What is the value of *pa + 3 ? ( 9) What happens when i = *pa++ evaluated? (pa=&a[4]) What is the value of i? ( 6) What happens when i = ++*pa evaluated? (++a[4]) What is the value of i? ( 9)

Pointers and Arrays An array name can be used in an expression the same way that a pointer can be in an expression (its actual value cannot be changed permanently) a + m is the same as &a[m] if pa = a, *(a + m) is the same as *(pa + m) *(pa + m) can be written as pa[m] Pointer[] -> the content, not the address Pa[4] == *(Pa + 4)

Examples – strlen ( ) We now discuss how “real” C programmers deal with strings in functions We can call strlen( ) with arguments that are an array or a pointer to an array of type char: strlen (arrayname) strlen (ptr) strlen("hello, world")

Examples – strlen Here is a variant of the way we did strlen ( ) int strlen(char s[ ]) { int n; for (n = 0; s[n]; n++) /*sum s+n, use as ptr */ ; /* for test, and increment n */ return n; }

Examples – strlen “Real” C programmers use pointers in cases like this: int strlen(char *s) /* s is just a copy of pointer */ { /* so no real change to string */ char *cp; for (cp = s; *s ; s++) /* no sum, just incr. pointer */ ; /* more efficient */ return s - cp; /* difference of pointers has int value*/ }

Examples – strlen Simplest form yet (See K&R, pg 39) int strlen (char *s) { char *p = s; while ( *p++) ; return p - s - 1; }

Examples – strcpy void strcpy ( char *s, char *t) /* copy t to s */ { while ( *s++ = *t++ ) /* stops when *t = '\0' */ ; /* look at page 105 examples */ } Note: there is an assignment statement inside the while ( ) (not just comparison) and the copy stops AFTER the assignment to *s of the final value 0 from *t

Examples - strcmp int strcmp ( char * s, char * t) /* space after * is OK */ { for ( ; *s == *t; s++, t++) if ( *s == '\0') return 0; /*have compared entire string and found no mismatch */ return *s - *t; /*on the first mismatch, return */ } /* (*s - *t is < 0 if *s < *t and is > 0 if *s > *t) */