C Praktikum. Advanced Pointers. Eugen Betke, Nathanael Hübbe, Michael Kuhn, Jakob Lüttgau, Jannek Squar

Similar documents
Memory (Stack and Heap)

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

void setup(){ void loop() { The above setup works, however the function is limited in the fact it can not be reused easily. To make the code more gene

Introduction to C: Pointers

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Midterm CSE 131B Spring 2005

Final CSE 131B Spring 2004

Embedded Controller Programming 2

Introduction to Linked Lists

Pointers and Arrays 1

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Tokens, Expressions and Control Structures

A brief introduction to C programming for Java programmers

QUIZ. What are 3 differences between C and C++ const variables?

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

G52CPP C++ Programming Lecture 12

C++ Important Questions with Answers

Computer System and programming in C

by Pearson Education, Inc. All Rights Reserved.

Type Checking. Prof. James L. Frankel Harvard University

Structures and Pointers

CS 61c: Great Ideas in Computer Architecture

Function I/O. Last Updated 10/30/18

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Midterm CSE 131 Winter 2014

Program Flow. Instructions and Memory. Why are these 16 bits? C code. Memory. a = b + c. Machine Code. Memory. Assembly Code.

We do not teach programming

CS 11 C track: lecture 5

Midterm CSE 131 Winter 2012

ECE 15B COMPUTER ORGANIZATION

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Algorithms & Data Structures

An application: foreign function bindings

Introduction to Core C++

CS 230 Programming Languages

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Lectures 13 & 14. memory management

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Introducing C++ David Chisnall. March 15, 2011

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Computer Science & Engineering 150A Problem Solving Using Computers

EL2310 Scientific Programming

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

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

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

EM108 Software Development for Engineers

CSC 211 Intermediate Programming. Arrays & Pointers

Reference operator (&)

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Character Strings. String-copy Example

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

Heap Arrays and Linked Lists. Steven R. Bagley

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

The New C Standard (Excerpted material)

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

IV Unit Second Part STRUCTURES

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Introduction to C++ with content from

Generic Swap. void swap (void *ap, void *bp, int size) { char temp[size]; memcpy (temp, ap, size); memcpy (ap, bp, size); memcpy (bp, temp, size); }

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

CS 222: Pointers and Manual Memory Management

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

Pointers. Pointers. Pointers (cont) CS 217

Intermediate Programming, Spring 2017*

First of all, it is a variable, just like other variables you studied

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

Memory, Arrays & Pointers

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/

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Lecture 2: C Programm

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

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Intro to C: Pointers and Arrays

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

COMP 2355 Introduction to Systems Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

CPSC 213, Winter 2009, Term 2 Midterm Exam Date: March 12, 2010; Instructor: Mike Feeley

C Programming Review CSC 4320/6320

FUNCTION POINTERS. int (*pf)(); // pf is a pointer to a function returning an int.

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

JTSK Programming in C II C-Lab II. Lecture 3 & 4

C Pointers. 6th April 2017 Giulio Picierro

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

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

Midterm CSE 131 Fall 2014

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

04-24/26 Discussion Notes

Midterm CSE 131 Winter 2013

Dynamic Memory Allocation and Linked Lists

Homework #3 CS2255 Fall 2012

CS 241 Data Organization Pointers and Arrays

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Review of the C Programming Language for Principles of Operating Systems

Transcription:

C Praktikum Advanced Pointers Eugen Betke, Nathanael Hübbe, Michael Kuhn, Jakob Lüttgau, Jannek Squar 2018-11-26

Warning This is a dive under the hood. We will see, and hopefully understand many details which still elude some seasoned programmers. Do not expect this presentation to align with what you expect, but expect to have some fun with the unexpected.

N.Hübbe Advanced Pointers in C 3/25 The Five 1 any object in C can be pointed to 2 pointer declarations are read inside out 3 a[b] * (a + b) and a >b (*a).b 4 value of ptr + a is ptr + a*sizeof(*ptr) 5 arrays are not pointers, they decay to pointers

N.Hübbe Advanced Pointers in C 4/25 Rule 1 any object in C can be pointed to Both, the type of any object and its associated pointer, can be written down in C. To turn a variable declaration into a pointer declaration, just add a * in front of the variable name. (An extra set of parentheses () may be needed.) Example time

N.Hübbe Advanced Pointers in C 5/25 Rule 2 pointer declarations are read inside out start at the variable name (type name for typedef) follow operator precedence array subscript ([]) and function call (()) take precedence over pointer dereference (*)

N.Hübbe Advanced Pointers in C 6/25 Rule 2 pointer declarations are read inside out Example 1: struct foo *(*bar)[5]; //bar is

N.Hübbe Advanced Pointers in C 7/25 Rule 2 pointer declarations are read inside out Example 1: struct foo *(*bar)[5]; //bar is struct foo *(*bar)[5]; //... a pointer to an

N.Hübbe Advanced Pointers in C 8/25 Rule 2 pointer declarations are read inside out Example 1: struct foo *(*bar)[5]; //bar is struct foo *(*bar)[5]; //... a pointer to an struct foo *(*bar)[5]; //... array of size 5 (precedence!),

N.Hübbe Advanced Pointers in C 9/25 Rule 2 pointer declarations are read inside out Example 1: struct foo *(*bar)[5]; //bar is struct foo *(*bar)[5]; //... a pointer to an struct foo *(*bar)[5]; //... array of size 5 (precedence!), struct foo *(*bar)[5]; //... elements are pointers to

N.Hübbe Advanced Pointers in C 10/25 Rule 2 pointer declarations are read inside out Example 1: struct foo *(*bar)[5]; //bar is struct foo *(*bar)[5]; //... a pointer to an struct foo *(*bar)[5]; //... array of size 5 (precedence!), struct foo *(*bar)[5]; //... elements are pointers to struct foo *(*bar)[5]; //... structs of type foo

N.Hübbe Advanced Pointers in C 11/25 Rule 2 pointer declarations are read inside out Example 2: void*(*foo[3])(int) //foo is an

N.Hübbe Advanced Pointers in C 12/25 Rule 2 pointer declarations are read inside out Example 2: void*(*foo[3])(int) //foo is an void*(*foo[3])(int) //... array of size 3 (precedence!),

N.Hübbe Advanced Pointers in C 13/25 Rule 2 pointer declarations are read inside out Example 2: void*(*foo[3])(int) //foo is an void*(*foo[3])(int) //... array of size 3 (precedence!), void*(*foo[3])(int) //... which are pointers to

N.Hübbe Advanced Pointers in C 14/25 Rule 2 pointer declarations are read inside out Example 2: void*(*foo[3])(int) //foo is an void*(*foo[3])(int) //... array of size 3 (precedence!), void*(*foo[3])(int) //... which are pointers to void*(*foo[3])(int) //... functions (precedence!),

N.Hübbe Advanced Pointers in C 15/25 Rule 2 pointer declarations are read inside out Example 2: void*(*foo[3])(int) //foo is an void*(*foo[3])(int) //... array of size 3 (precedence!), void*(*foo[3])(int) //... which are pointers to void*(*foo[3])(int) //... functions (precedence!), void*(*foo[3])(int) //... which take an int argument

N.Hübbe Advanced Pointers in C 16/25 Rule 2 pointer declarations are read inside out Example 2: void*(*foo[3])(int) //foo is an void*(*foo[3])(int) //... array of size 3 (precedence!), void*(*foo[3])(int) //... which are pointers to void*(*foo[3])(int) //... functions (precedence!), void*(*foo[3])(int) //... which take an int argument void*(*foo[3])(int) //... and return a void*

N.Hübbe Advanced Pointers in C 17/25 Rule 3 a[b] * (a + b) and a >b (*a).b Example time

N.Hübbe Advanced Pointers in C 18/25 Rule 4 value of ptr + a is ptr + a*sizeof(*ptr) Example time

N.Hübbe Advanced Pointers in C 19/25 Rule 5 arrays are not pointers, they decay to pointers only & and sizeof operators do not trigger decay decay happens even in function declarations (because argument passing is a use) Example time

N.Hübbe Advanced Pointers in C 20/25 The Five 1 any object in C can be pointed to 2 pointer declarations are read inside out 3 a[b] * (a + b) and a >b (*a).b 4 value of ptr + a is ptr + a*sizeof(*ptr) 5 arrays are not pointers, they decay to pointers Warning: Only Rule 2 and Rule 5 hold true in C++.

N.Hübbe Advanced Pointers in C 21/25 Multidimensional Arrays Passing Multidimensional Arrays to Functions Three methods available - what are the differences? Example time

N.Hübbe Advanced Pointers in C 22/25 Multidimensional Arrays Storing Multidimensional Arrays in Objects Pitfall: Can t store pointer to array of dynamic size in struct. Must use untyped pointer and casts. Example time

N.Hübbe Advanced Pointers in C 23/25 Function Pointers Function Pointers as Callbacks Whenever a callback comes in handy... Example time

N.Hübbe Advanced Pointers in C 24/25 Function Pointers Function Pointers for Customizable Behavior Idea: Store function pointer in struct to make function call runtime decision polymorphic objects! But that s for another day...

N.Hübbe Advanced Pointers in C 25/25 Summary Summary only 5 simple pointer rules...... that allow us to do complex stuff real dynamic 2D arrays (envious, C++?) function pointers make function calls runtime decisions (callbacks and polymorphism)