C Pointers. 6th April 2017 Giulio Picierro

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

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

Pointers, Dynamic Data, and Reference Types

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

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

Exam 3 Chapters 7 & 9

Arrays and Pointers in C. Alan L. Cox

What is an algorithm?

Homework #3 CS2255 Fall 2012

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

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

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

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

Algorithms & Data Structures

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.

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

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.

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Procedural Programming

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to Pointers in C. Address operator. Pointers. Readings: CP:AMA 11, 17.7

Introduction to C++ with content from

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

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

Lecture 05 POINTERS 1

Introduction to Computer Science Midterm 3 Fall, Points

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

Lecture 2: C Programm

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Variation of Pointers

Dynamic Allocation in C

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

Variables, Memory and Pointers

Understanding Pointers

What are the most likely declarations of "month" in the old and new versions of the program?

Tutorial 10 Pointers in C. Shuyue Hu

Introduction to Programming Using Java (98-388)

pointers + memory double x; string a; int x; main overhead int y; main overhead

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

COMP1917: 09 Arrays and Strings

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1

Programming Studio #9 ECE 190

REFERENCES, POINTERS AND STRUCTS

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

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

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

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

CSCE 206: Structured Programming in C++

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

In Java we have the keyword null, which is the value of an uninitialized reference type

Goals of this Lecture

Launchpad Lecture -10

Fundamental of Programming (C)

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

DECLARAING AND INITIALIZING POINTERS

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

[0569] p 0318 garbage

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Week 3: Pointers (Part 2)

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

Pointers and Dynamic Memory Allocation

PROGRAMMAZIONE I A.A. 2017/2018

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

CMSC202 Computer Science II for Majors

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Pointers. 10/5/07 Pointers 1

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

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

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

FORM 2 (Please put your name and form # on the scantron!!!!)

Constants, References

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

CMSC 341 Lecture 2 Dynamic Memory and Pointers

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

APSC 160 Review Part 2

Vector and Free Store (Pointers and Memory Allocation)

CS1150 Principles of Computer Science Methods

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Object-Oriented Programming, Iouliia Skliarova

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

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

Transcription:

C Pointers 6th April 07 Giulio Picierro <giulio.picierro@uniroma.it>

Functions Return type Function name Arguments list Function body int sum(int a, int b) { return a + b; } Return statement (return keyword + value)

Functions Function argument pass By value void my_function(int a) { a++; } int main(void) { int b = 0; my_function(b); printf("b=%d\n", b); } A copy of variable is made. Modifying a variable inside a function, DOES NOT modify variable outside.

Functions Function argument pass By reference No copy is made: a reference is passed Modifying a variable inside called function, MODIFY variable outside C doesn t support this, but you can pass pointers!

Data type that represents a memory address Typically pointers to variables, i.e. address of variables. Declaration: or: T* var_name; T *var_name; How to read it: Pointer to T

Data type that represents a memory address You can retrieve address of a variable using the & operator Memory 48 int num = 5; int *num_addr = &num; 5 56 60 5 987 num num_addr 5 64 68 3 5

Dereference You can retrieve pointed value using the * operator Memory int a = *num_addr; a 5 5 5 56 987 Here, a is a new variable which holds a copy of num. This means that address of a is different from address of num num_addr 5 48 60 64 68 3 5 num

Dereference You can change pointed value using the * operator Memory *num_addr = 3; 5 3 num_addr 5 48 56 60 64 68 987 3 5 num

Functions Function argument pass By address : Address of variable passed by value void my_function(int *a) { (*a)++; } int main(void) { int b = 0; my_function(&b); printf("b=%d\n", b); } Increase value pointed by a (dereference) Passing memory address of b.. Passing by reference doesn t exists in C

NULL A pointer with address value of 0 is considered NULL pointer Special value => Point to nothing => Invalid pointer Memory 48 5 5 num int *num_addr = NULL; 56 60 987 num_addr 0 64 68 3 5

Dereference Dereference a NULL pointer is an error! Memory int *num_addr = NULL; 48 int a = *num_addr; // the program will crash 5 56 5 987 num 60 num_addr 0 64 68 3 5

Arithmetic Different from integer arithmetic int num = 5; 48 5 56 60 64 68 Memory 5 987 3 5 num

Arithmetic Different from integer arithmetic int num = 5; num++; 48 5 56 60 64 68 num now is 6 Memory 6 987 3 5 num

Arithmetic Different from integer arithmetic int num = 5; num++; Pointer arithmetic int *num_addr = &num; 48 5 Memory 6 num 56 987 60 num_addr 5 64 68 3 5

Arithmetic Different from integer arithmetic int num = 5; num++; Pointer arithmetic int *num_addr = &num; num_addr++; 48 5 56 Memory 6 987 num 60 Pointer is increased by 4!!! num_addr 56 64 68 3 5

Arithmetic Given a pointer T* Increase a pointer by one equals to increase by sizeof(t) num_addr 56 48 5 56 60 64 68 Memory 6 987 3 5

Arithmetic Given a pointer T* Increase a pointer by one equals to increase by sizeof(t) char *a = ; short *b = ; double *c = ; a++; // a now is 3 b++; // b now is 4 c++; // c now is 0 num_addr 56 48 5 56 60 64 68 Memory 6 987 3 5

Pointers and Arrays Same values: array &(array[0]) array[i] *(array + i) Different types: array is an int[7] array+i is an int* 6 0 4 8 3 36 array 3 5 8 3 array[0] array[] array[] array[3] array[4] array[5] array[6]

Pointers and Arrays array int array[] = {,,, 3, 5, 8, 3}; 6 array[0] int* ptr = array; ptr 6 0 4 array[] array[] 8 3 array[3] 3 5 array[4] 36 8 array[5] 3 array[6]

Pointers and Arrays array int array[] = {,,, 3, 5, 8, 3}; 6 array[0] int* ptr = array; ptr++; ptr 0 0 4 array[] array[] 8 3 array[3] 3 5 array[4] 36 8 array[5] 3 array[6] NOTE: Pointers have their arithmetic, increase a pointer T* by one, means increase address by sizeof(t)

Pointers and Arrays array int array[] = {,,, 3, 5, 8, 3}; 6 array[0] int* ptr = array; ptr++; ptr 0 4 array[] array[] ptr = array + 6; 8 3 array[3] 3 5 array[4] 36 8 array[5] 3 array[6] NOTE: Pointers have their arithmetic, increase a pointer T* by one, means increase address by sizeof(t)

Pointers and Arrays array int array[] = {,,, 3, 5, 8, 3}; 6 array[0] int* ptr = array; ptr++; ptr 0 4 array[] array[] ptr = array + 6; 8 3 array[3] *ptr = ; 3 5 array[4] 36 8 array[5] array[6] NOTE: Pointers have their arithmetic, increase a pointer T* by one, means increase address by sizeof(t)

Pointers and Arrays array int array[] = {,,, 3, 5, 8, 3}; 6 array[0] int* ptr = array; ptr++; ptr 4 0 4 array[] array[] ptr = array + 6; 8 3 array[3] *ptr = ; 3 5 array[4] ptr = &array[]; 36 8 array[5] array[6] NOTE: Pointers have their arithmetic, increase a pointer T* by one, means increase address by sizeof(t)

Pointers and Arrays array int array[] = {,,, 3, 5, 8, 3}; 6 array[0] int* ptr = array; ptr++; ptr 4 0 4 7 array[] array[] ptr = array + 6; 8 3 array[3] *ptr = ; 3 5 array[4] ptr = &array[]; ptr[0] = 7; 36 8 array[5] array[6] NOTE: Pointers have their arithmetic, increase a pointer T* by one, means increase address by sizeof(t)

Arrays and functions Arrays are always passed by base address 6 0 array array[0] array[] void function(int v[]) { v[3] = 7; } 4 8 3 3 5 array[] array[3] array[4] 36 8 array[5] 3 array[6]

Arrays and functions Arrays are always passed by base address 6 0 array array[0] array[] void function(int v[]) { v[3] = 7; } 4 8 3 7 5 array[] array[3] array[4] function(array); 36 8 array[5] You re not copying the array, but the base address!!! Change an element of the array inside the function equals to change the element of the original array!!! 3 array[6]

Arrays and functions Arrays are always passed by base address 6 0 array array[0] array[] int* function(void) { int array[] = {,, 3, 4, 5}; } return array; 4 8 3 36 3 5 8 array[] array[3] array[4] array[5] Returning a pointer to a local variable IS AN ERROR! Since local variables lives on the stack, the memory of those variables could be overwritten by subsequent function calls! 3 array[6]

Pointers and Const const int* p; *p = 5; // compiler error p = NULL; // ok int* const p; *p = 5; // ok p = NULL; // compiler error () () Read from right to left: ) Pointer to int const ) Const pointer to int 3) Const pointer to int const const int* const p; *p = 5; // compiler error p = NULL; // compiler error (3)