Introduction to Pointers

Slides:



Advertisements
Similar presentations
An introduction to pointers in c
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
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.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Arrays and Pointers in C Alan L. Cox
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
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++
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.
Week 2 - Wednesday CS 121.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User-Written Functions
Chapter 2 Basic Computation
EGR 2261 Unit 10 Two-dimensional Arrays
CSE 374 Programming Concepts & Tools
Motivation and Overview
More important details More fun Part 3
Introduction to Computer Science / Procedural – 67130
C Programming Tutorial – Part I
Testing and Debugging.
Multiple variables can be created in one declaration
Variables and Primative Types
COSC 220 Computer Science II
Pointers and Memory Overview
Student Book An Introduction
Java Review: Reference Types
Arrays in C.
Pointers and References
Lecture 6 C++ Programming
Pointers and References
Introduction to Pointers
Introduction to Pointers
Pointers and References
Object Oriented Programming COP3330 / CGS5409
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Instructor: Ioannis A. Vetsikas
Chapter 16 Pointers and Arrays
Pointers and Arrays.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Chapter 16 Pointers and Arrays
Overloading functions
Introduction to Primitives
Introduction to Primitives
Data Structures and Algorithms Introduction to Pointers
CISC181 Introduction to Computer Science Dr
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

Introduction to Pointers Adapted from Dr. Craig Chase, The University of Texas at Austin

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.

Recall Variables Recall a variable is nothing more than a convenient name for a memory location. The variable’s scope defines when/if the memory location can be re-used (e.g., two different local variables in different subroutines may use the same memory location at different times). The type of the variable (e.g., int) defines how the bits inside that memory location will be interpreted, and also define what operations are permitted on this variable. Every variable has an address. Every variable has a value.

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 (hence the FP and activation record stuff). There are 4 billion (232) 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.

Byte Addresses Most computers are “byte addressable” That means that each byte of memory has a distinct address Most variable types require more than one byte The “address” of a variable is the address of the first byte for that variable

Padding and Alignment The compiler and linker are not obligated to store variables in adjacent locations (except for arrays) The compiler might request “padding” between small variables The hardware may be faster at loading large pieces of data from some addresses than others Choosing an address where the least significant k bits are zero is called “alignment to a 2k-byte boundary”

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 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; x = 0; while (*p == x) { print x; *p = *p + 1; }

Characteristics of Pointers We’ve seen that pointers can be initialized and assigned (like any variable can). They can be local or global variables (or parameters) You can have an array of pointers etc., just like any other kind of variable. We’ve also seen the dereference operator (*). This is the operation that really makes pointers special (pointers are the only type of variable that can be dereferenced).

Comparing Pointers Pointers can also be compared using ==, !=, <, >, <=, and >= 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 (more on this later).

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 kth 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.

Pointer Arith. Example: int numbers[10]; int* p = &numbers[0]; int first_address = p; int last_address; while (p != &numbers[10]) { *p = 42; p = p + 1; } last_address = p; NOTE: last_address == first_address + 40

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]

More Pointers and Arrays To ensure that we achieve maximal confusion, the name of an array can be used to substitute for the address of the first variable. int stuff[10]; // stuff == &stuff[0] This innocent looking rule means that arrays can not be passed as arguments to functions!!!! Instead of passing the array, one passes the address of the first variable. doit(stuff); // “stuff” is the address of the first variable The pointer parameter can be declared as a normal pointer, or using the (extremely misleading) syntax: int doit(int x[10]) { // x is really a pointer int doit(int* x) { // same thing! int doit(int x[]) { // you can do this too x is a ptr.

Huh? Is there really something tricky going on with arrays as parameters? Sure, try this: void doit(int a, int x[10]) { a = x[0] = 42; } void main(void){ int nums[10] = { 0 }; int k = 17; doit(k, nums); Note that k is not changed, but nums[0] is set to 42!

Syntax Tricks The C-language support for arrays is really quite limited. In effect, C doesn’t support arrays at all, just pointers and pointer arithmetic. (that’s why we’re avoiding 2D arrays). Think about it, if x is the name of an array. Then: int x[10]; int* p = x; x[3] = 2; p[3] = 42; // same, in fact same as *(p + 3) = 42 So, the C support for arrays is limited to declaring them. Everything else is really pointers!

Proof void doit(int x[10], int* p) { // two pointers int y[10]; // a real array of ten variables x += 1; // legal, x is really a pointer variable *x = 42; // legal p += 2; // legal *p = 3; // legal y += 1; // illegal, y is not a variable! y[i] is a variable! *y = 5; // legal, ‘cause *y is same as *(&y[0]) } Keep in mind that the name of a (real) array is “an expression evaluating to the address of the first variable”. A little like saying “3 is an expression evaluating to the number 3. You can’t say in C: “3 = 10;” So, you can’t say “y = &y[1];”

Row-Major Ordering and 2D C stores arrays declared as two- dimensional using a one-dimensional array (of course). The first elements stored are those in the first row (in order). Then, the second row is stored, etc. This memory allocation policy is called “row- major ordering”. If we want to access a variable in row i and column j, then that variable is located at the following offset from the start of the array. i * num_columns + j;

“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”.

When would you point to nothing? Imagine writing a function findIt that returns a pointer to the first occurrence of the letter ‘z’ in a string. What should you return if there are no ‘z’s in the string? How about the address zero? char* findIt(char* str) { while (*str != ‘\0’) { if (*str == ‘z’) { return str; } str += 1; } return 0; //Don’t do this use NULL

Using Null Pointers By convention, a pointer who’s value is the address zero is called a “null pointer”. Use the value NULL and not the value 0. The literal “0” can be assigned to a pointer without making the compiler grumpy, but it’s best to use NULL. Note that an integer variable or any other constant cannot be assigned without a “type cast”. Many people get confused between a pointer who’s value is the address zero, and a pointer that points to a variable with the value zero.

Observations The first key is to understand what it means to “dereference” something. The next key is to understand why some expressions can’t be dereferenced. The last key is to understand the auto-scaling that’s performed during pointer arithmetic.

What is x? Can only be answered in context x = 42; x is really a location. Usually when we ask this question, we mean “what is the value of x” Sometimes, we really mean the location x = 42; In this context, x is a memory location.

What is *x *x is a memory location To know which memory location, we need to know the value of x For example, if x is 3, then *x is location 3 If *x is on the left hand side of an assignment, then we will store a value into location x If *x appears on the right hand side of an assignment, then we are talking about the value of location x

What does * mean? *(anything) means Figure out the value of “anything” and Use that memory location for whatever it is that we’re trying to do *3 = 6; // put the value 6 into location 3 *5 = *3; // put 6 (the value in location 3) into location 5 If there are two (or more) *s, then just apply them from right-to-left **3 = 42; // find out the value of location 3, (6 in our example), then put 42 into location 6