Pointers. andy = 25; fred = andy; ted = &andy; andy = 25; ted = &andy; beth = *ted;

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2.
For(int i = 1; i
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Functions Prototypes, parameter passing, return values, activation frams.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Introduction to Programming Lecture 39. Copy Constructor.
File and I/O system calls int open(const char* path, int flags, mode_t modes) int creat(const char *path, mode_t mode) ssize_t read(int fd, void *buf,
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
1 DATA ABSTRACTION: USER DEFINED TYPES AND THE CLASS.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
Single Right rotation (compare to RotateWithLeftChild page 148 text) void rotateRight(AVLNode *& curr) { AVLNode * kid = curr->left; curr->left = kid->right;
Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
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);
CS 240: Data Structures Supplemental: Command Line Input.
 Review structures  Program to demonstrate a structure containing a pointer.
LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.
C++ Character Set It is set of Characters/digits/symbol which is valid in C++. Example – A-Z, (white space) C++ Character Set It is set of.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
LOOPING IN C. What would be the output of the following program main( ) { int j ; while ( j
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 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Introduction to Programming Lesson 3. #include #include main ( ) { cout
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
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.
Programming with ANSI C ++
Virtual Functions Use a single pointer variable to refer to the objects of different classes. That is the base class pointer. The compiler ignores the.
Pointers & Arrays.
Introduction to Programming
Command Line Arguments
Andy Wang Object Oriented Programming in C++ COP 3330
Basic notes on pointers in C
Pointers  Week 10.
OBJECTIVE QUESTIONS.
Pointers & Functions.
מצביעים וכתובות מבוא כללי למדעי המחשב
Local Variables, Global Variables and Variable Scope
Default Arguments.
Dynamic Memory A whole heap of fun….
5.1 Introduction Pointers Powerful, but difficult to master
C++ Language Tutorial Second Class First Semester Lecturers:
Introduction to Programming
Function Overloading.
C Programming Lecture-8 Pointers and Memory Management
Pointers & Arrays.
Pointers & Functions.
The Stack.
Pointers and dynamic objects
Presentation transcript:

Pointers

andy = 25; fred = andy; ted = &andy;

andy = 25; ted = &andy; beth = *ted;

int * numberPtr; char * characterPtr; float * greatnumberPtr; int * mPtr, * nPtr, *j;

int main () { int firstvalue, secondvalue; int * mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; cout << "firstvalue is " << firstvalue << endl; cout << "secondvalue is " << secondvalue << endl; return 0; } firstvalue is 10 secondvalue is 20

int main (){ int firstvalue = 5, secondvalue = 15; int * p1, * p2; p1 = &firstvalue; //p1 = address of firstvalue p2 = &secondvalue; //p2 = address of secondvalue *p1 = 10; //value pointed by p1 = 10 *p2 = *p1; //value pointed by p2=value pointed by p1 p1 = p2; // p1 = p2 (value of pointer is copied) *p1 = 20; // value pointed by p1 = 20 cout << "firstvalue is " << firstvalue << endl; cout <<"secondvalue is " << secondvalue << endl; return 0;} firstvalue is 10 secondvalue is 20

int main () { int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n<5; n++) cout << numbers[n] << ", "; return 0; } 10, 20, 30, 40, 50,

 Pointer to specific address: int number; int *tommy = &number;  Pointer to nothing int *tommy = NULL; equivalents to int *tommy = 0;

 Strings as pointer to characters char * terry = "hello"; The fifth element can be accessed with: *(terry+4) or terry[4]

 Suppose the following piece of code: char *mychar; short *myshort; long *mylong; mychar++; myshort++; mylong++;  (++) and (--) operators have greater operator precedence than the dereference operator (*).

void main () { int a = 50; int *aptr ; aptr = &a; // assume that aptr=0x0018FF44 cout <<"The output of a= "<<a << "\n"; cout <<"The output of *aptr = "<<*aptr << "\n"; cout <<"The output of &a= "<<&a << "\n"; cout <<"The output of &*aptr = "<<&*aptr << "\n"; cout <<"The output of *&aptr = "<<*&aptr << "\n"; }

 Write a C++ program that defines array b with 10 integer elements. Then the program should use pointers to set each element in the array to ZERO.