Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.

Slides:



Advertisements
Similar presentations
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
Programming and Data Structure
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
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.
Week 8 Arrays Part 2 String & Pointer
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Kernighan/Ritchie: Kelley/Pohl:
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Introduction to C Programming CE
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Pointers CSE 2451 Rong Shi.
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Dynamic memory allocation and Pointers Lecture 4.
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
+ 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.
Variables and memory addresses
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Week 12 Methods for passing actual parameters to formal parameters.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
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.
A Sample Program #include using namespace std; int main(void) { cout
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers What is the data type of pointer variables?
Introduction to Programming Using C
CSE 220 – C Programming Pointers.
Pointers and Pointer-Based Strings
Pointer.
Basic notes on pointers in C
Alternate Version of STARTING OUT WITH C++ 4th Edition
Simulating Reference Parameters in C
Pointers and Pointer-Based Strings
Chapter 9: Pointers and String
Pointers and dynamic objects
Presentation transcript:

Prepared by Andrew Jung

Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and initialization of the array */ int table [6] = {16, 21, 8, 3, -7, 9}; /* Declaration of a pointer to an integer. */ int *ptr /* The pointer, which is an address, is assigned the address of the first address of the first cell in the array. */ ptr = &table [0] Result of coding is illustrated in the following diagram. Array Name: Table /*Following printf function will display “16”*/ cout<<*ptr ptr = ptr+1 (or ptr++)  causes the pointer to the second cell in the array ptrptr+1ptr+2ptr+3ptr+4ptr

Accessing Pointer Data When an array is declared, the name of the array is actually a pointer to the first element in the array. As an consequence array parameters are always passed to a function by reference. table[6] = {16, 21, 8, 3, -7, 9} ----> This is the same as following: *(table + 0) = 16 *(table + 1) = 21 *(table + 2) = 8 *(table + 3) = 3 *(table + 4) = -7 *(table + 5) = 9 But table[0] =16, table[1] =21, table[2]=8, table[3]=3, table[4]=-7 table[5]=9 Therefore, (table + i) is the same as the table[i]. where i = 0 to 5.

Accessing Pointer Data #include using namespace std; int main() { char a[4] ="HAL"; cout<<a[0]<<"\n"; //display 0th element of an array cout<<*a<<"\n"; //a is a character array. it prints 'H' which is first element of an array cout<<a; //a is a pointer to an array of characters cout<<"\n"; cout<<*(a+1)<<"\n"; cout<<*(a+2)<<"\n"; return 0; }

Accessing Pointer Data Example:

In-Class Activity 7 On Moodle…

Pointers as Function arguments and Parameters Two way to pass variables to a function by reference By using & smbol prior to the parameter in the function head By passing an array using an array name.

Pointers as Function arguments and Parameters #include using namespace std; void swap(int *p1, int *p2); int main() { int value1 = 10; int value2 = 20; int *pointer2 = &value2; //int *pointer1 = &value1; cout<<"The value before the function call: "<<value1<<" "<<value2<<endl; //swap(pointer1, pointer2); swap(&value1, pointer2); cout<<"The value after the function call: "<<value1<<“ "<<value2<<endl; return 0; } void swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; }

Pointers as Function arguments and Parameters #include using namespace std; void Display_String(char *String); int main() { char *String = "HELLO"; Display_String(String); return 0; } void Display_String(char *String) { cout<<"The string value is: "<<String; }

Array of Pointers We can easily define an array that stores pointers, thus creating an array of pointers Example: char *ptr_Array[2]; ptr_Array[0] = “Dog”; ptr_Array[1] = “Cat”; First, define an array of pointers to character data. Since array size is two, this array holds two pointers, or addresses. Since an array name is always a pointer and this pointer points to the pointers contained in the array.

Array of Pointers ‘D’‘o’‘g’‘\0’ Address of “Dog” Ptr_Array[0] Ptr_Array *(Ptr_Array[0]) *(Ptr_Array[0]+1) Address of “Dog” Ptr_Array[1] ‘C’‘a’‘t’‘\0’ *Ptr_Array *(Ptr_Array[1]) *(Ptr_Array+1) *(Ptr_Array[1]+2)

In-Class-Activity 8 On Moodle…