Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
CS61C L4 C Pointers (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 –C Strings,
Pointers CSE 2451 Rong Shi.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Pointers Gabriel Hugh Elkaim Spring 2012.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Lecture 23: Pointers. 2 Lecture Contents: t Pointers and addresses t Pointers and function arguments t Pointers and arrays t Pointer arrays t Demo programs.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Computer science C programming language lesson 3.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Object Oriented Programming Lecture 2: BallWorld.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
CprE 185: Intro to Problem Solving (using C)
CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev
CprE 185: Intro to Problem Solving (using C)
CprE 185: Intro to Problem Solving (using C)
CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev
Memory, Data, & Addressing II CSE 351 Summer 2018
Beginning C for Engineers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CprE 185: Intro to Problem Solving (using C)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev
Instructor: Alexander Stoytchev
CprE 185: Intro to Problem Solving (using C)
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Administrative Stuff HW 10 due this 8pm Extra credit HW is due this 8pm There will be labs next week Last lecture is on Monday Next Wednesday we have a review for the final

Final Exam Final Exam: Thursday Dec 17: 2:15- 4:15 pm

Pointers (part 2) CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Chapter (?!?)

What is a pointer?

Memory Analogy

Pointers: Mailbox Analogy

A letter fits comfortably in this box

A parcel does not. So, they give you a key …

… the key opens a larger mailbox …

… the parcel is stored there.

This is the pointer to the parcel.

What is a pointer to a pointer?

Pointer to a Pointer (Analogy)

First Key opens #9 Second Key opens the parcel box

What is an array of pointers?

Array of Pointers (Analogy)

Pointer Arithmetic

Memory Analogy Address 0 Address 1 Address 2 Address 3 Address 4 Address 5 Address 6

Memory Analogy (32 bit architecture) Address 0 Address 4 Address 8 Address 12 Address 16 Address 20 Address 24

Memory Analogy (32 bit architecture) Address 0x00 Address 0x04 Address 0x08 Address 0x0C Address 0x10 Address 0x14 Address 0x18 Hexadecimal Address 0x0A Address 0x0D

The pointer is incremented by the size of the thing to which it points Address 0x00 Address 0x04 Address 0x08 Address 0x0C Address 0x10 Address 0x14 Address 0x18 Memory for variable i int i; int* ip = &i; ip ip +1 An int pointer is incremented by 4 bytes (i.e., 1*sizeof(int) )

The pointer is incremented by the size of the thing to which it points Address 0x00 Address 0x04 Address 0x08 Address 0x0C Address 0x10 Address 0x14 Address 0x18 Memory for variable d double d; double* dp = &d; dp dp +1 An double pointer is incremented by 8 bytes (i.e., 1*sizeof(double) )

Basic pointer operations int i = 5; Int* ip = &i; printf("%d\n", *ip); [

Basic pointer operations *ip = 7; [

Basic pointer operations int j = 3; ip = &j; [

Basic pointer operations int *ip2; ip2 = ip; [

Basic pointer operations ip = &i; [

Pointers and arrays Is there a difference?

Pointers and arrays Array indexing: a[0], a[1], a[2], … int *ip = &a[0]; Pointer indexing: *ip, *(ip+1), *(ip+2), … In general a[i] is “equivalent” to *(ip+i)

Pointer Arithmetic ip2 = ip1 + 3; ip2 - ip1 == 3 (true or false?)

int *ip; int a[10]; ip = &a[3]; ip2 = ip + 1; [

autoincrement operator ++ (and its companion, --) Both of these are defined for pointers Array version: a[i++] Pointer version: *ip++

Prefix form is defined too Array version: a[++i] preincrement form: *++ip *ip-- and *--ip.

Copying an array using pointers int array1[10], array2[10]; int *ip1, *ip2 = &array2[0]; int *ep = &array1[10]; for(ip1 = &array1[0]; ip1 < ep; ip1++) *ip2++ = *ip1; [

Comparing strings using pointers char *p1 = &str1[0], *p2 = &str2[0]; while(1) { if(*p1 != *p2) return *p1 - *p2; if(*p1 == '\0' || *p2 == '\0') return 0; p1++; p2++; } [

String copy using pointers char *dp = &dest[0], *sp = &src[0]; while(*sp != '\0') *dp++ = *sp++; *dp = '\0'; [

Arrays of pointers #include int main() { int* pArray[10]; int a; int b; pArray[0] = &a; pArray[1] = &b; system("pause"); }

Pointers to functions // function returning an int // and having two arguments (an int and a char) int fun1(int a, char c); // function returning pointer to an int // and having one int argument int *fun2(int a); // pointer to function returning int // and having two arguments (an int and a char) int (*funp)(int a, char c); // two ways to call the function (*funp)(1,’b’); funp(1,’c’);

Example #include int fun1(int a, int b) { printf("fun1\n"); return a+b; } int fun2(int a, int b) { printf("fun2\n"); return a-b; } int main() { // pointer to function returning int and having two arguments: an int and a float int (*funp)(int a, int b); funp = fun1; // take the address of the function and assign it to the function pointer (*funp)(1,2); // call the function using the pointer funp = fun2; // reassign the pointer to point to fun2 funp(1,2); // an alternative way of calling a function using a pointer system("pause"); }

Pointers to Structures #include typedef struct node { int value; } node_t; int main() { node_t Node; Node.value = 5; // initialize it to 5 printf("value = %d\n", Node.value); // pointer to the statically allocated struct Node node_t *p = &Node; p->value = 6; // change it to 6 printf("value = %d\n", p->value); }

THE END