Topics. Introduction to pointers Pointers and function parameters Pointers Arithmetic

Similar documents
arrays C Programming Language - Arrays

Sources: On the Web: Slides will be available on:

The C Programming Language course syllabus associate level

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Object Oriented Software Design II

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

5 Arrays and Pointers

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Stacks. Linear data structures

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

C Interview Questions

MIPS Assembly Code Layout

C PROGRAMMING FOR MATHEMATICAL COMPUTING

C++ Programming Language

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

X86-64 Architecture Guide

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

El Dorado Union High School District Educational Services

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Informatica e Sistemi in Tempo Reale

Chapter 13 Storage classes

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

System Calls and Standard I/O

Pointers and Memory. By Nick Parlante

Chapter 7D The Java Virtual Machine

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Implementation Aspects of OO-Languages

Answers to Review Questions Chapter 7

Keil C51 Cross Compiler

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Comp151. Definitions & Declarations

Java Interview Questions and Answers

Machine Programming II: Instruc8ons

IC4 Programmer s Manual

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Introduction to Information Security

1 Abstract Data Types Information Hiding

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Module 816. File Management in C. M. Campbell 1993 Deakin University

Chapter One Introduction to Programming

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Number Representation

Assembly Language: Function Calls" Jennifer Rexford!

Illustration 1: Diagram of program function and data flow

Programming languages C

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Chapter 2: Elements of Java

CSCE 465 Computer & Network Security

Data Structures using OOP C++ Lecture 1

Principles of Database Management Systems. Overview. Principles of Data Layout. Topic for today. "Executive Summary": here.

C++FA 3.1 OPTIMIZING C++

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

System Calls Related to File Manipulation

Parameter Passing in Pascal

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

Application Security: Web service and

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

How To Write Portable Programs In C

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Object Oriented Software Design II

Organization of Records in Blocks

Conditionals (with solutions)

IPC. Semaphores were chosen for synchronisation (out of several options).

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

Semantic Analysis: Types and Type Checking

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Introduction to Java

CmpSci 187: Programming with Data Structures Spring 2015

Pemrograman Dasar. Basic Elements Of Java

ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials

So far we have considered only numeric processing, i.e. processing of numeric data represented

The Function Pointer

Chapter 2 Introduction to Java programming

Simple C Programs. Goals for this Lecture. Help you learn about:

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

Embedded C Programming

VB.NET Programming Fundamentals

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

An Incomplete C++ Primer. University of Wyoming MA 5310

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Introduction to Programming II Winter, 2014 Assignment 2

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

CP Lab 2: Writing programs for simple arithmetic problems

Tutorial on C Language Programming

Transcription:

Pointers

Topics Introduction to pointers Pointers and function parameters Pointers Arithmetic

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

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

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

Pointers A variable which can store the memory address of another variable 0x3A5 0x000 chptr 0xFFE 0xFFF 0x000 0x00 0x00 B ch etc 6

Pointers A pointer is a variable Contains a memory address Points to a specific data type 7

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

Pointers and the & Operator Example: char c = A ; char *cptr; cptr = &c; Assigns the address of c to cptr c: cptr: A 0x000 0x000 0x004 9

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; 0

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; You can print the address stored in a pointer using the %p conversion specifier Example: printf( %p, numptr);

The * Operator Allows pointers to access variables they point to Also known as dereferencing operator Should not be confused with the * in the pointer declaration

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: BA NULL 0x000 0x000 0x004 3

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

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

Easy Steps to Pointers (cont) 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 = # 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: 6

Easy Steps to Pointers (cont) Step 4: De-reference the pointers int num; char ch = A ; float x; int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; numptr = # chptr = &ch; xptr = &x; *xptr = 0.5; *numptr = *chptr; numptr: addr of num chptr: xptr: addr of ch addr of x num: 65 ch: A x: 0.5 7

Notes on Pointers (cont) int *numptr; Beware of pointers which are not initialized!??? numptr 8

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 9

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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); 35

Pointer arithmetic C allows pointer values to be incremented by integer values char *m =?dog?; d (char) o (char) g (char) NUL (char) char result = *(m + ); (char *) m m gives an address of a char (m + ) gives the char one byte higher *(m + ) instructs us to take the contents of that address result gets the value 'o' o (char) result 36

Pointer arithmetic A slightly more complex example: char *m =?dog?; d (char) o (char) g (char) NUL (char) char result = *++m; m gives an address of a char ++m changes m, to the address one byte higher, and returns the new address *++m instructs us to take the contents of that location result gets the value 'o' (char *) m o (char) result 37

Pointer arithmetic How about multibyte values? Q: Each char value occupies exactly one byte, so obviously incrementing the pointer by one takes you to a new char value... But what about types like int that span more than one byte? A: C does the right thing : increments the pointer by the size of one int value 7 (int) 4 (int) int a[] = 7, 4; int *m = a; int result = *++m; (int *) m 4 (char) (int) result 38

Example: strcpy string copy char *strcpy(char *dest, const char *src) const char *p; char *q; for(p = src, q = dest; *p!= '\0'; p++, q++) *q = *p; *q = '\0'; return dest; (char *) src d (char) o (char) g (char) NUL (char) p (char *) (char *) q 39 (char *) dest d o (char) (char) (char) (char) g NUL