1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in.

Slides:



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

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Introduction to Pointers These Slides NOT From Text.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
CS150 Introduction to Computer Science 1
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
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. + 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.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
UNIT 8 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
Windows Programming Lecture 03. Pointers and Arrays.
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.
Hank Childs, University of Oregon April 6 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Intro to Pointers in C CSSE 332 Operating Systems
EGR 2261 Unit 11 Pointers and Dynamic Variables
EGR 2261 Unit 10 Two-dimensional Arrays
UNIT 5 C Pointers.
Pointers.
Pointers and Pointer-Based Strings
Student Book An Introduction
Introduction to the C Language
Pointers.
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
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
Variables and Computer Memory
Introduction to Pointers
Introduction to Pointers
Presentation transcript:

1 CS 201 Introduction to Pointers Debzani Deb

2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in either side of you. Calculators that have text-allowing memory is not permitted. Covers Chapter 1-6 (Lecture Materials, Readings, Quiz, Linux basics, Makefile, Eclipse, Pointer & Labs). You should be able to answer correctly 80-85% of the exam, if you get hold of the above materials. The rest % will be challenging. me/TA any question you have or see us at our office times. Don’t just wait for the last minute.

3 What is a Pointer? A pointer is a variable which points to a place in computer’s memory.  It contains memory address as its value ( POINTER = ADDRESS)  Pointer variable: a variable dedicated to hold memory addresses. A variable name directly references a value (i.e. content of a memory cell). A pointer indirectly references a value. Referencing a value through a pointer is called indirection. x int p int * content Address

4 Pointers and Addresses Addr1 Addr2 Addr3 Addr4 Addr5 Addr6... Addr11... Addr15 Content1 Content2 Content11 Content15 Memory can be conceptualized as a linear sequence of addressable locations. Variable reference the content of a location. Pointers have a value of the address of a location.

5 Size of Variable Types Everything in a computer program must fit into high speed memory. High speed memory is simply a string of bytes. Lots and Lots and Lots of bytes, but just that…bytes. Some number of bytes are allocated every time you define a variable.

6 High Speed Memory (RAM) Gb of RAM

7 So What Is The Size Of Different Types? printf(“sizeof(int) = %d\n”, sizeof(int)); If you ran a short program with a bunch of these statements. sizeof(int) = 4 sizeof(long) = 4 sizeof(long int) = 4 sizeof(long long int) = 8 sizeof(char) = 1 sizeof(float) = 4 sizeof(double) = 8 sizeof(long double) = 12

8 So What Is The Size Of Different Types? sizeof(int) = 4 sizeof(long) = 4 sizeof(long int) = 4 (long long illegal on WinXp C++.Net) sizeof(char) = 1 sizeof(float) = 4 sizeof(double) = 8 sizeof(long double) = 8 Now, on to pointers! Depends on hardware, brand of OS and compiler. Here is Visual Studio.Net on Windows Xp Professional. (Differences are marked in red.)

9 Again What is Pointer? A variable containing the address of another variable. (Hence it “points” to the other location). There are “things” and “pointers to things” 3 thing thing_ptr ReferenceExplanationValue thingDirect value of the variable thing3 thing_ptrDirect value of variable thing_ptrPointer to location containing 3 *thing_ptrIndirect value of thing_ptr3

10 Structure Of A Pointer Symbol Table 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C int x=2,y=4,z=6; memory Address shown in hex. name address

11 Structure Of A Pointer Symbol Table X Y 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C Z int x=2,y=4,z=6; memory name address

12 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x=2,y=4,z=6; 2 memory 4 6 name address Note, I have shown the value of the variable in memory, not the actual representation!

13 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x=2,y=4,z=6; int * px; px = &x; memory name address

14 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px memory px 0F45AB F45AB14 name address int x=2,y=4,z=6; int * px; px = &x;

15 So, what is the size of a pointer? sizeof(int *) = 4 sizeof(long long int *) = 4 sizeof(float *) = 4 sizeof(double *) = 4 sizeof (char * ) = 4 Pointers to different types are all the same size. Once again, their size may be dependent on your hardware, OS, or even compiler. Now, back to my diagram.

16 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px memory px 0F45AB F45AB14 name address int x=2,y=4,z=6; int * px; px = &x;

17 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px 0F45AB24 memory px 0F45AB14 printf(“%d\n”,x); name address int x=2,y=4,z=6; int * px; px = &x;

18 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px 0F45AB24 memory px 0F45AB14 printf(“%d\n”,x);2 name address int x=2,y=4,z=6; int * px; px = &x;

19 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px 0F45AB24 memory px 0F45AB14 printf(“%d\n”,x);2 printf(“%d\n”,*px); int x=2,y=4,z=6; int * px; px = &x;

20 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x,y,z; int * px = &x; px 0F45AB24 memory px 0F45AB14 printf(“%d\n”,x);2 printf(“%d\n”,*px); 2

21 Pointer Declaration int * p; // pointer is declared p is a pointer pointing at an variable of type int. * tells the compiler that the variable named p is to be a pointer. The name of the pointer variable is p (not *p ). The size of the memory addressed by a pointer depends on the type of the object mentioned in declaration. int * p; // p points at 4 consecutive bytes double * p; // p points at 8 consecutive bytes

22 Operators Related To Pointers & address-of  printf(“%p\n”,&variable); * dereference  printf(“%d\n”, * pointer_variable)

23 Address of Operator (&) int x = 3; // Variable defined int * p; // Pointer declared p = &x; // Pointer p is set up to point at // the variable x. Address of variable x is assigned to pointer p. & is an unary address operator which returns the memory address of variable. &x is read as “address of x”

24 About Asterisk (*) In declaration  * tells the compiler that a new variable is to be a pointer (read as “pointer to”) In Regular use  It provides the content of the memory location specified by a pointer. It mean “follow the pointer”.  It can also stand on the left side of an assignment. int * p x = * p * p = 10

25 Typical Uses Of Operators int thing; /* declare an integer (a thing) */ thing = 4; int * thing_ptr; /* declare a pointer to an integer */  Don’t need _ptr, just helps readability.  At this moment, it is pointing to a random values since it has not been initialized yet. Such garbage can be harmful. thing_ptr = NULL;  thing_ptr is a null pointer means “pointing to nothing”. thing_ptr = &thing; /* store address in pointer, points to thing */

26 Cont. *thing_ptr = 5; /* set thing to 5 */  *thing_ptr is a thing.  thing_ptr is a pointer. Of course, thing_ptr could be set to point to other variables besides thing.  thing_ptr = &something_else;

27 Illegal and Strange Uses *thing is illegal. thing is not a pointer variable. &thing_ptr is legal, but strange. “a pointer to a pointer”

28 Pointing to the Same Thing int something; int *first_ptr; /* one pointer */ int *second_ptr; /* another pointer */ something=1; first_ptr = &something; second_ptr = first_ptr; something=1; *first_ptr=1; *second_ptr=1; All identical results (Only one integer)

29 Again uses of & and * p = &x; // p points at x now. y = * p; // y has the value pointed out by the pointer p. * p= 13; // 13 was inserted to the // place pointed out by p. x int p int * y int x int p int *