ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Applications
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
November 18, 2015Memory and Pointers in Assembly1 What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; }
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Computer Organization and Design Pointers, Arrays and Strings in C
Pointers in C.
Pointers and Pointer-Based Strings
Student Book An Introduction
Basic notes on pointers in C
Pointers and References
Lecture 18 Arrays and Pointer Arithmetic
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Simulating Reference Parameters in C
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Overloading functions
Pointers and Pointer-Based Strings
Chapter 9: Pointers and String
Pointers and References
C Parameter Passing.
Presentation transcript:

ENEE150 – 0102 ANDREW GOFFIN More With Pointers

Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic Goffin – ENEE150

Arrays as Pointers Arrays are made of data laid out contiguously Array name IS a pointer to the first element in array Goffin – ENEE int a[8] = {1,2,3,4,5,6,7,8}; a *a == 1 and a[0] == 1

Pointer Arithmetic Can move through memory by adding to pointers float *a, *p; p = a+1; // how many bytes does this move in memory? If current address is next to address with data, can access this data!  This is the case with arrays, since arrays are stored contiguously.  a[1] == *(a+1) Goffin – ENEE150

Pointer Arithmetic Examples An integer pointer, p, stores the address 0x100. An array of length 9 stores the following values: 1,2,3,4,5,6,7,8,9, starting at address 0x100. Answer the following, assuming that an int is 4 bytes: a. What is the address of the number 5 in the array? b. *(p+6) is what value? c. p+3 is what value? d. What is &(p[3])? Goffin – ENEE150

Pointer Arithmetic Notes Only makes sense with arrays  The only time values are DEFINITELY stored contiguously in memory  Otherwise, good chance you’ll get garbage Again, *(p+i) is interchangable with p[i]  Also, &(p[i]) == p+i Goffin – ENEE150

Passing By Reference Function parameters are inherently “pass by value”  It copies the value itself into the function  This can be computationally expensive for large data types  Also cannot vary value outside function Pass By Reference -> Pass the address  Instead of passing a variable, pass a pointer to a variable!  The pointer itself is passing by value, but you can dereference to get the original variable’s contents  Always a simple 8 bytes (size of pointer)  Can modify contents of address  Arrays are always pass by reference… array names are pointers! Goffin – ENEE150

Midterm #1 – 10/6/15 Open note, open book Open Ended Questions and Coding Questions Topics:  C language features  ENEE140  Functional Decomposition  Testing  Debugging  Pointers  More specifics in handout