Computer Programming Lecture 12 Pointers

Similar documents
Slides adopted from T. Ferguson Spring 2016

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

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

Computer Programming Lecture 13 Functions with Multiple Output Parameters

Tutorial 10 Pointers in C. Shuyue Hu

Introduction to Scientific Computing and Problem Solving

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

Introduction to Computing Lecture 09: Functions (Part II)

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Introduction to Computing Lecture 03: Basic input / output operations

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

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

Decimal Representation

Introduction to Computing Lecture 10 Arrays (Part 1)

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

At the end of this module, the student should be able to:

Lecture 04 Introduction to pointers

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

Lecture 9 - Pointers 1

Arrays and Pointers. Lecture Plan.

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

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?

More Pointers Week 11

CSC 211 Intermediate Programming. Pointers

CS 2461: Computer Architecture I

Chapter 7 C Pointers

More about BOOLEAN issues

Chapter 16 Pointers and Arrays

Cpt S 122 Data Structures. Data Structures

n Address of a variable in memory n Allows us to indirectly access variables ! Array n A list of values arranged sequentially in memory

Introduction to Computing Lecture 08: Functions (Part I)

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

... Lecture 12. Pointers

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

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

Chapter 16 Pointers and Arrays

Introduction to Computing Lecture 05: Selection (continued)

MYcsvtu Notes LECTURE 34. POINTERS

Self-referential Structures and Linked List. Programming and Data Structure 1

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Pointers and Arrays

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

AMCAT Automata Coding Sample Questions And Answers

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

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

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Overview (1A) Young Won Lim 9/9/17

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

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

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Arrays and Pointers (part 1)

Introduction to Computing Lecture 01: Introduction to C

EL2310 Scientific Programming

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

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

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

Pointers and Output Parameters

BSM540 Basics of C Language

DECLARAING AND INITIALIZING POINTERS

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

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

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

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Dynamic memory allocation (malloc)

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

CS240: Programming in C

CMSC202 Computer Science II for Majors

EM108 Software Development for Engineers

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Week 3: Pointers (Part 2)

Pointer Data Type and Pointer Variables

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

Pointers in C/C++ 1 Memory Addresses 2

[0569] p 0318 garbage

Computer Programming

Arrays and Pointers (part 1)

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

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

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.

Functions. Arash Rafiey. September 26, 2017

Lecture 3: C Programm

COMP s1 Lecture 1

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1

Overview (1A) Young Won Lim 9/14/17

C++ for Java Programmers

Week 7: Pointers and Arrays. BJ Furman 02OCT2009

Pointers. 10/5/07 Pointers 1

Subject: Fundamental of Computer Programming 2068

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

Unit 7. Functions. Need of User Defined Functions

Lesson 7. Reading and Writing a.k.a. Input and Output

CS240: Programming in C

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Overview (1A) Young Won Lim 9/25/17

PROGRAMMAZIONE I A.A. 2017/2018

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Transcription:

Computer Programming Lecture 2 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr

Topics Introduction to Pointers Pointers and Function Parameters

Introduction Pointer is one of the most powerful features of C Pointers enable programs to simulate call by reference to create and manipulate dynamic data structures (that can grow and shrink)

Computer Memory Computer s memory consists of many thousands of sequential storage locations, and each location is identified by a unique address From 0 to maximum When you declare a variable in a C program, the compiler set aside a memory location with a unique address to store that variable When your program uses the variable name, it automatically accesses the proper memory location

Memory Address of a Variable char ch = A ; ch: 0x2000 A The memory address of the variable ch. The value of the variable ch.

The & Operator Gives the memory address of an object char ch = A ; 0x2000 A &ch yields the value 0x2000 Also known as the address operator.

Example: char ch; printf( %p, &ch); conversion specifier for printing a memory address

Pointers A variable which can store the memory address of another variable. 0x3A5 0x2000 chptr 0xFFE 0xFFF 0x2000 0x200 0x2002 B ch etc.

Pointers A variable directly references a value A pointer indirectly references a value count 7 countptr count 7

Pointers A pointer is a variable Contains a memory address Points to a specific data type Pointer variables are usually named varptr To declare a pointer, we use the following form typename *ptrname;

Example: cptr: char* cptr; 0x2004 Can store an address of variables of type char We say cptr is a pointer to char.

Pointers and the & Operator Example: char c = A ; char *cptr; cptr = &c; Assigns the address of c to cptr. c: cptr: 0x2000 A 0x2004 0x2000

Notes on Pointers We can have pointers to any data type. Example: int* numptr; float* xptr; The * can be anywhere between the type and the variable. Example: int *numptr; float * xptr; char* chptr;

Notes on Pointers (cont.) You can assign the address of a variable to a compatible pointer using the & operator. Example: int anumber; int *numptr; numptr = &anumber;

Notes on Pointers (cont.) You can print the address stored in a pointer using the %p conversion specifier. Example: printf( %p, numptr);

Notes on Pointers (cont.) When declaring a pointer, it is a good idea to always initialize it to NULL (a special pointer constant). int *numptr = NULL; NULL numptr

The * Operator Allows pointers to access to variables they point to Also known as dereferencing or indirection operator Should not be confused with the * in the pointer declaration or the multiplication operator Don't worry about the compiler's becoming confused

Pointers and the Operator Example: char c = A ; char *cptr = NULL; cptr = &c; *cptr = B ; Changes the value of the variable which cptr points to. c: cptr: 0x2000 B A 0x2004 0x2000

int rate; Pointers and the Operator int *p_rate; rate = 00; p_rate = &rate; printf( %d,rate); printf( %d,*p_rate); 00 00

Pointer Cautions int *numptr; Beware of pointers which are not initialized! *numptr = 2;??? numptr

Easy Steps to Pointers Step : Declare the variable to be pointed to. int num; char ch = A ; float x; num: ch: A x:

Easy Steps to Pointers Step 2: Declare the pointer variable. int num; char ch = A ; float x; numptr: NULL int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; chptr: xptr: NULL NULL num: ch: A x:

Easy Steps to Pointers Step 3: Assign address of variable to pointer. int num; char ch = A ; float x; int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; numptr = &num; chptr = &ch; xptr = &x; numptr: addr of num chptr: xptr: num: ch: addr of ch addr of x A A pointer s type has to correspond to the type of the variable it points to. x:

Easy Steps to Pointers Step 4: De-reference the pointers. int num; char ch = A ; float x; int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; numptr = &num; chptr = &ch; xptr = &x; *xptr = 0.25; *numptr = *chptr; numptr: addr of num chptr: xptr: addr of ch addr of x num: 65 ch: A x: 0.25

Pointers and Function Parameters Example: Function to swap the values of two variables: x: y: 2 swap x: 2 y:

#include <stdio.h> Bad swap void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; main() int x =, y = 2; swap(x, y); printf( %d %d\n, x, y);

#include <stdio.h> Bad swap void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> Good swap void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; main() int x =, y = 2; swap2(&x, &y); printf( %d %d\n, x, y);

#include <stdio.h> Good swap void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; main() int x =, y = 2; x: swap2(&x, &y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; tmp: a: b: Good swap addr of x addr of y main() int x =, y = 2; x: swap2(&x, &y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; tmp: a: b: Good swap addr of x addr of y main() int x =, y = 2; x: swap2(&x, &y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; tmp: a: b: Good swap addr of x addr of y main() int x =, y = 2; x: 2 swap2(&x, &y); printf( %d %d\n, x, y); y: 2

#include <stdio.h> void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; tmp: a: b: Good swap addr of x addr of y main() int x =, y = 2; x: 2 swap2(&x, &y); printf( %d %d\n, x, y); y:

#include <stdio.h> Good swap void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; main() int x =, y = 2; x: 2 swap2(&x, &y); printf( %d %d\n, x, y); y:

Pointers and Function Arguments Change the value of an actual parameter variable. scanf demystified. char ch; int numx; float numy; scanf( %c %d %f, &ch, &numx, &numy);