Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
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:
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Pointers Applications
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
String Escape Sequences
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to Computer Systems and the Java Programming Language.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Lecture #5 Introduction to C++
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
Introduction to Java Java Translation Program Structure
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; }
Data Types Declarations Expressions Data storage C++ Basics.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
EGR 2261 Unit 11 Pointers and Dynamic Variables
UNIT 5 C Pointers.
Student Book An Introduction
INC 161 , CPE 100 Computer Programming
Java Review: Reference Types
Introduction to Pointers
Introduction to Pointers
Unit 2 Programming.
2.1 Parts of a C++ Program.
Pointers and References
Introduction to Primitives
Introduction to Primitives
Pointer Arithmetic By Anand George.
Introduction to Pointers
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Pointer

Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages (e.g., Java, Pascal) do not arm the programmer with as many pointer operations (e.g., pointer arithmetic) Your best defense is to understand what pointers really mean and how they really work.

The Real Variable Name is its Address! When your program uses a variable the compiler inserts machine code that calculates the address of the variable. Only by knowing the address can the variables be accessed. There are 4 billion (2 32 ) different addresses, and hence 4 billion different memory locations. Each memory location is a variable (whether your program uses it or not). Your program will probably only create names for a small subset of these “potential variables”. Some variables are guarded by the operating system and cannot be accessed.

Addresses and Pointers A pointer variable is a variable! –It is stored in memory somewhere and has an address. –It is a string of bits (just like any other variable). –Pointers are 32 bits long on most systems. –The value of a pointer must be set by assigning to the pointer (and can be changed by assigning a different value – just like any other type of variable). The bits inside a pointer are interpreted as the address of another variable. –This other variable can be accessed using the pointer, instead of using the variable’s name.

Syntax Declaring a pointer: int* p; –Declares a variable p of type “pointer that holds the address of an int variable”. Calculating the address of a variable and storing it in a pointer: p = &x; –x is an int variable. “&x” is an expression that evaluates to the address of x. –The assignment to p is just normal assignment (after all, p is just a variable, right?).

De-Reference If p holds the address of another variable x we can now access that variable in one of two ways: –using the name of the variable: x = 42; –“dereferencing” the pointer: *p = 42; NOTE: both of these assignments mean exactly the same thing (provided, of course, that the current value of p is the address of x). A dereferenced pointer can substitute for the variable – anywhere. *p and x mean exactly the same thing.

Examples of Pointers Assume: int x = 100; int *p ; The same pointer can “point to” multiple variables (not at the same time, of course): p = &x; // p points to x x = x + *p; doubles x p = &y; // now p points to y *p = 42; // y is set to 42 (x is unchanged). An infinite loop (obviously): p = &x; while (*p == x) { *p = *p + 1; }

Comparing Pointers Pointers can also be compared using ==, !=,, = –Two pointers are “equal” if they point to the same variable (i.e., the pointers have the same value!) –A pointer p is “less than” some other pointer q if the address currently stored in p is smaller than the address currently stored in q. –It is rarely useful to compare pointers with < unless both p and q “point” to variables in the same array.

Pointer Arithmetic Pointers can be used in expressions with addition and subtraction. These expressions only make sense if the pointer points at one of the variables in an array! Adding an integer value k to a pointer p calculates the address of the k th variable after the one pointed to by p. –if p == &x[0] then p + 4 == &x[4]; –Negative integers can also be added (same as subtracting a positive). Subtracting two pointers calculates the (integer) number of variables between the pointers.

Pointer “Size” Note: Pointers are all the same size. On most computers, a pointer variable is four bytes (32 bits). –However, the variable that a pointer points to can be arbitrary sizes. –A char* pointer points at variables that are one byte long. A double* pointer points at variables that are eight bytes long. When pointer arithmetic is performed, the actual address stored in the pointer is computed based on the size of the variables being pointed at.

Pointers and Arrays Pointers and arrays are absolutely not the same things! –A pointer is one variable. –An array is a collection of several variables Unfortunately, C syntax allows pointers to be used in similar ways as arrays. –Specifically, for any integer i and pointer p the following two expressions reference the same variable: *(p + i) p[i]

“Null” Pointers C gives the OS and compiler a lot of freedom with addresses: –e.g., Variables can have funky alignment, for example many char variables “use up” 4 bytes However, one address is special: address zero. –No variable or function can be stored at address zero. –It is never legal to store a value into the memory location at address zero (doing so results in a runtime error, AKA “Core Dump”). The reason that zero is reserved is so that programmers can use this address to indicate a “pointer to nothing”.

NULL Pointer The identifier NULL is defined to be zero in several of the standard header files including stdio.h. NULL has a special use with pointers. It can be assigned to all types of pointer variables. pointervar = NULL

Pointer Examples address2a.c address3.c arrayptr.c pointer.c pointer2.c