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.

Slides:



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

An introduction to pointers in c
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers and References (1) THE REFERENCE OPERATOR REFERENCES POINTERS THE DEREFERENCE OPERATOR DERIVED TYPES RETURNING A REFERENCE.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Pointers Applications
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Data Types.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Pointers CSE 2451 Rong Shi.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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.
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.
Dynamic memory allocation and Pointers Lecture 4.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Variables and memory addresses
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
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).
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Introduction to programming in java Lecture 21 Arrays – Part 1.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Windows Programming Lecture 03. Pointers and Arrays.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Pointers What is the data type of pointer variables?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers Psst… over there.
Pointer Basics Psst… over there.
Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.
Introduction to Programming
Windows Programming Lecture 02.
Lecture 18 Arrays and Pointer Arithmetic
Pointers Lecture 1 Thu, Jan 15, 2004.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
Pointer Basics Psst… over there.
CS148 Introduction to Programming II
Presentation transcript:

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. b But the value that is stored in a pointer is always the address of another memory location.

The Use of Pointers in C b Pointers are used in function calls. b Pointers are used with arrays and strings. b Pointers are used to access files. b Pointers are used with structures.

Syntax for Pointer Declarations and Assignments b Pointer variables can be declared in programs and then used to take addresses as values: int i, *p;int i, *p; declares i to be a variable of type int and p to be a variable of type “pointer to int”. p = &i;p = &i; assigns the address in memory named by i to the pointer variable p.

Some Groundwork for an Example b Let’s state some assumptions that we will use in an example. Every byte in our computer’s RAM (random access memory) is numbered sequentially starting with zero.Every byte in our computer’s RAM (random access memory) is numbered sequentially starting with zero. –We refer to this number as the address of a particular byte. When we declare a variable we give it a type and a name.When we declare a variable we give it a type and a name. –We can think of the name as a name for the beginning address of the bytes that the system assigns to the declared variable -- how many total bytes are assigned (they will be sequential bytes) depends on the type of the variable.

Example Declaration and Assignment int i = 25, j = 13, *p; int i = 25, j = 13, *p; three memory locations have been chosen by the system (assume they are the following): three memory locations have been chosen by the system (assume they are the following): is the address named i is the address named p is the address named j 2513 nothing assigned

Now Some New Assignments i = j; p = & i; /* & is the address operator */ is the address named i is the address named p is the address named j p now “points” to i (the value stored in p is the address of i ). i = j; p = & i;

Using p to Change What is in i is the address named i is the address named p is the address named j *p = 35; /* Used this way, * is called the dereference */ /* (or indirection)operator. The meaning here */ /* is to assign 35 to the memory location */ /* stored in p. */

Understanding Pointers b The previous examples have used “made up” memory addresses and values to explain pointer declaration and pointer dereferencing.

Evaluating Pointer Expressions Declarations and initializations int i = 3, j = 5, *p = &i, *q = &j, *r; double x; Expression Equivalent expression Value p == &i p == (&i) 1 p = i + 7 p = (1 + 7) illegal * *&p *(*(&p)) 3 r = &x r = &x illegal 7 * * p / *q + 7 (((7 * (*p))) / (*q)) * (r = &j) *= *p (*(r = (&j))) *= (*p) 15

Pointers to void b In ANSI C, one pointer can be assigned to another only when: They both have the same type, orThey both have the same type, or When one of them is of type pointer to void (void *).When one of them is of type pointer to void (void *). b We can think of void * as a generic pointer type.

Illegal and Legal Pointer Assignments Declarations int *p; float *q; void *v; Legal Assignments Illegal Assignments p = 0; p = 1; p = (int *) 1; v = 1; p = v = q; p = q; p = (int *) q;