Introduction to Pointers These Slides NOT From Text.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
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.
Introduction to C Programming CE
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Arrays and Pointers in C Alan L. Cox
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.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
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:
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
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 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.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
UNIT 8 Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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.
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?
CS1010 Programming Methodology
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 7 - Pointers Outline 7.1 Introduction
2016.
Pointers and Pointer-Based Strings
Pointers.
Student Book An Introduction
Lecture 6 C++ Programming
Fundamental Data Types
Dynamic Memory Allocation
void Pointers Lesson xx
Pointers.
Introduction to the C Language
Lecture 18 Arrays and Pointer Arithmetic
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers Pointers point to memory locations
Fundamental Data Types
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
DATA TYPES There are four basic data types associated with variables:
Pointer Arithmetic By Anand George.
Presentation transcript:

Introduction to Pointers These Slides NOT From Text

First … size of 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.

High Speed Memory (RAM) Gb of RAM

So What Is The Size Of Different Types? printf(“sizeof(int) = %d\n”, sizeof(int)); I 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

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 brand of OS and compiler. Here is Visual Studio.Net on my Windows Xp Professional. (Differences are marked in red.)

What is a pointer? A variable containing the address of another variable. (Hence it “points” to the other location). There are “things” and “pointers to things”

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

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

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!

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 = &x; memory name address

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 memory px 0F45AB F45AB14 name address

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. Now, back to my diagram.

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 memory px 0F45AB F45AB14 name address

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); name address

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 name address

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);

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

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

Pointer Operator Syntax thing Simple Thing (variable) &thingPointer to variable thing thing_ptr Pointer to an integer *thing_ptr Integer

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. Doesn’t initialize to any value yet. (garbage) thing_ptr = &thing; /* store address in pointer, points to thing */

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, (it just wouldn’t be as understandable). thing_ptr = &something_else;

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

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)

Pass By Reference Pass the address of a variable by value to a function and dereference it inside the function. i.e. Pass a pointer by value to a function and dereference it inside the function.

Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; }

Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; } countcount_ptr

Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; } countcount_ptr 0

Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; } countcount_ptr 0 Address of count passed to function. (received as count_ptr)

Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; } countcount_ptr 0 Address of count passed to function. (received as count_ptr) Increments the count variable in the main program.