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

Similar documents
Lecture 05 POINTERS 1

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

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

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

Dynamic Data Structures. CSCI 112: Programming in C

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

Cpt S 122 Data Structures. Data Structures

Chapter 6 - Pointers

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

by Pearson Education, Inc. All Rights Reserved.

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

C++ Programming Chapter 7 Pointers

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

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

Pointers and Strings. Adhi Harmoko S, M.Komp

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Chapter 7 C Pointers

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

MYcsvtu Notes LECTURE 34. POINTERS

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

Algorithms & Data Structures

Programming for Engineers Pointers

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS201- Introduction to Programming Current Quizzes

C++ for Java Programmers

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.

CPSC 3740 Programming Languages University of Lethbridge. Data Types

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

LOÏC CAPPANERA. 1. Memory management The variables involved in a C program can be stored either statically or dynamically.

EL2310 Scientific Programming

Understanding Pointers

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

Intermediate Programming, Spring 2017*

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

CSC 211 Intermediate Programming. Pointers

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Pointers, Dynamic Data, and Reference Types

Contents of Lecture 3

Type Checking. Prof. James L. Frankel Harvard University

CSCI 171 Chapter Outlines

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

Procedural programming with C

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

DECLARAING AND INITIALIZING POINTERS

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Short Notes of CS201

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

[0569] p 0318 garbage

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

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

CS201 - Introduction to Programming Glossary By

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

Dynamic Memory Allocation

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Dynamic Allocation in C

SYSC 2006 C Winter 2012

Dynamic Memory Allocation and Command-line Arguments

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

Lecture Notes on Memory Management

Dynamic Memory Allocation

Dynamic memory allocation

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

Pointers in C/C++ 1 Memory Addresses 2

Review of the C Programming Language

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Lecture 8 Dynamic Memory Allocation

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

Pointer Data Type and Pointer Variables

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Review of the C Programming Language for Principles of Operating Systems

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

C Pointers Pearson Education, Inc. All rights reserved.

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamic memory allocation (malloc)

Pointers II. Class 31

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

(1-2) Introduction to C Data Structures & Abstract Data Types. Instructor - Andrew S. O Fallon CptS 122 (June 6, 2018) Washington State University

Lecture 3: C Programm

Pointers and Terminal Control

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Arrays, Pointers and Memory Management

POINTER AND ARRAY SUNU WIBIRAMA

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Run-time Environments - 3

CSC 1600 Memory Layout for Unix Processes"

CS 11 C track: lecture 5

CSC 211 Intermediate Programming. Arrays & Pointers

CSC C69: OPERATING SYSTEMS

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Low-Level C Programming. Memory map Pointers Arrays Structures

Programming Language Implementation

Computer Programming C++ (wg) CCOs

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

Transcription:

1 Programming in C

Pointer Variable A variable that stores a memory address Allows C programs to simulate call-by-reference Allows a programmer to create and manipulate dynamic data structures Must be defined before it can be used Should be initialized to NULL or valid address 2

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

Pointers A pointer variable has two associated values: Direct value address of another memory cell Referenced by using the variable name Indirect value value of the memory cell whose address is the pointer's direct value. Referenced by using the indirection operator * 4

Pointer Operators Come before a variable name * operator Indirection operator or dereferencing operator Returns a synonym, alias or nickname to which its operand points & operator Address of operator Returns the address of its operand 5

Pointer Variables One way to store a value in a pointer variable is to use the & operator The address of count is stored in countptr We say, countptr points to count 6

Pointer Variables Assume count will be stored in memory at location 700 and countptr will be stored at location 300 causes 5 to be stored in count causes the address of count to be stored in countptr 7

Pointer Variables We represent this graphically as 8

Pointer Variables The indirection / dereferencing operator is * stores the value 10 in the address pointed to by countptr 9

Pointer Variables The character * is used in two ways: 1. To declare that a variable is a pointer Pre-pending a variable with a * in a declaration declares that the variable will be a pointer to the indicated type instead of a regular variable of that type 2. To access the location pointed to by a pointer Pre-pending a variable with a * in an expression indicates the value in the location pointed to by the address stored in the variable 10

Simulating By Reference Invoked function uses * in formal parameters Invoking function uses & in actual parameters 11

Pointer Variables and Arrays Given The compiler will know how many bytes to copy into the memory location pointed to by xptr Defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code 12

Pointer Variables and Arrays Consider a block in memory consisting of ten integers of 4 bytes in a row at location 100 10 Now, let's say we point an integer pointer aptr at the first of these integers What happens when we write? 13

Pointer Variables and Arrays Because the compiler "knows" This is a pointer (i.e. its value is an address) That it points to an integer of length 4 at location 100 Instead of 1, adds 4 to aptr Now aptr "points to" the next integer at location 104 Same for: aptr+=1, aptr++, and ++aptr 14

Pointer Variables and Arrays Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, this brings up an interesting relationship between arrays and pointers 15

Pointer Variables and Arrays Consider this array allocated at location 200 We have an array containing 10 integers We refer to each of these integers by means of a subscript to scores Using scores[0] through scores[9] 16

Pointer Variables and Arrays The name of an array and the address of the first element in the array represent the same thing Consequently, we could alternatively access them via a pointer: 17

Pointer Variables and Arrays The name of an array is a pointer constant to the first element of the array So, we could also use : 18

Pointer Arithmetic and Arrays If scoreptr is pointing to a specific element in the array and n is an integer, scoreptr + n is the pointer value n elements away We can access elements of the array either using the array notation or pointer notation If scoreptr points to the first element, the following two expressions are equivalent: scores[n] Array notation *(scoreptr + n) Pointer notation 19

Pointers and Dynamic Allocation of Memory So far, we have always allocated memory for variables that are located on the stack Size of such variables must be known at compile time Sometimes convenient to allocate memory at run time System maintains a second storage area called the heap Functions calloc and malloc allocate memory as needed of size needed 20

Pointers and Dynamic Allocation of Memory 1. Use allocating function (such as malloc(), calloc(), etc.) Returns void pointer void * indicates a pointer to untyped memory Will have to cast the returned value to the specific type needed 2. Use memory through the pointer notation 3. Release allocated space when no longer needed, so that it can be reused 21

Pointers and Dynamic Allocation of Memory: calloc calloc Used to dynamically create an array in the heap Contiguous allocation Initialized to binary zeros Must Takes two arguments 1. Number of array elements 2. Amount of memory required for one element Use sizeof function / operator Returns Void pointer if successful NULL if unsuccessful 22

Pointers and Dynamic Allocation of Memory: calloc Example 1: String Example 2: Integers 23

Pointers and Dynamic Allocation of Memory: malloc malloc Used to dynamically get memory from heap Contiguous allocation No initialization Must Takes one argument Total amount of memory required Returns Void pointer if successful NULL if unsuccessful 24

Pointers and Dynamic Allocation of Memory: malloc Example 1: String Example 2: Integers 25

Pointers and Dynamic Allocation of Memory: free free Used to dynamically release memory back to heap Contiguous deallocation Must Takes one argument Pointer to beginning of allocated memory Good idea to also NULL pointer if reusing 26

Pointers and Dynamic Allocation of Memory: free Example 2 with free 27

Programming in C T H E E N D 28