Chapter 8 Arrays, Strings and Pointers

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
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.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers Applications
Computer Science 210 Computer Organization Pointers.
Pointers CSE 2451 Rong Shi.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
© 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.
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 and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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. 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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Computer Science 210 Computer Organization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 10: Pointers Starting Out with C++ Early Objects
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers.
Pointers.
CNG 140 C Programming (Lecture set 10)
INC 161 , CPE 100 Computer Programming
Module 2 Arrays and strings – example programs.
POINTERS.
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointers.
Computer Science 210 Computer Organization
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Pointer-Based Strings
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers Kingdom of Saudi Arabia
Outline Defining and using Pointers Operations on pointers
Pointers.
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
Data Structures and Algorithms Introduction to Pointers
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Programming in C Pointers and Arrays.
Pointers and dynamic objects
CISC181 Introduction to Computer Science Dr
Introduction to Pointers
Presentation transcript:

Chapter 8 Arrays, Strings and Pointers 8b: Pointers

Goals: Pointers & pointer operators Functions: Reference Parameter Pointers & Arrays Arrays of strings

Introduction - Variable Variable is a space in memory that is used to hold a value Each variable has a name, content, and address Variable name is a logical reference Address is a physical reference Variable name (logical reference) Address (physical reference)

Introduction - Variable Accessing content (value) & address int main() { int n = 33; printf("n = %d\n", n); //print the value printf(“&n = %d\n", &n); //print the address return 0; }

Introduction - Variable A char uses 1 byte int main() { char achar[100] = "\nABCDEFGxyz123"; printf("%s\n", achar); printf("%d\n", &achar[0]); printf("%d\n", &achar[1]); return 0; } Print the address Output: ABCDEFGxyz123 2686636 2686637

Introduction - Variable An int uses 4 bytes The address of an int variable is the first byte int main() { int aint[5] = {0, 1, 2, 3, 4}; printf("%d\n", &aint[0]); printf("%d\n", &aint[1]); return 0; } Print the address Output: 2686716 2686720

Introduction - Pointers Variable: A data variable contains a value (eg. Integer number, a real number or a character) . Pointers: Pointer is another type of variable that contains the address of other variable. It also known as address variable.

type_name *pointer_name; Name of the pointer variable Introduction - Pointers Pointer Declaration: Example: type_name *pointer_name; Pointer’s base type - determines what type of data the pointer will be pointing to Name of the pointer variable - determines what type of data the pointer will be pointing to int* ip; float* fp; ip to be a pointer to an int (ip can be used to point to int values) fp can be used to point to float values

Introduction - Pointers Pointer Initialization:

Introduction - Pointers Using Pointer: Example: int a = -123; //a is a data variable int *p; //p is a pointer variable p = &a; //p then holds the address of variable a

Pointer Operators Two special operators: &: reference pointer (address operator) returns the memory address of its operand ptr = &total; *: dereference pointer (indirection operator) returns the value of the variable located at the address specified by its operand val = *ptr;

Pointer Operators Two special operators: Example: Pointer value, reference and dereference int main() { int a = -123; int* p = &a; //p holds the address of a printf("n = %d\n", a); printf("&n = %d\n", &a); printf("p = %d\n", p); //pointer value – address of a printf("&p = %d\n", &p);//reference – address of p printf("*p = %d\n", *p); //dereference – value of p return 0; }

Pointer Operators Two special operators: Example: p and q points to the same variable int a = -123; //a is a data variable int *p; //p is a pointer variable int *q; //q is a pointer variable p = &a; //p holds the address a q = p; //q = p =address of a

Pointer Operators Two special operators: Question: What will be the output? int main() { int a = 11; int b = 0; int* p1 = &a; int* p2 = &b; printf("%d %d\n", *p1, *p2); p2 = p1; a = 12; b = 1; return 0; }

Assigning Values through Pointers Assign a value to the location pointed to by the pointer Example Assigns value 101 to the location pointed to by p *p = 101; To increment (or decrement) the value at the location pointed to by a pointer: (*p)++; Note: * operator has lower precedence than ++ operator

Assigning Values through Pointers Example: int main() { int *p; int num; p = # *p = 100; //assign num the value 100 through pointer p printf("%d\n", num); (*p)++; //increment num through p (*p)--; //decrement num through p return 0; }

Pointer Arithmetic Pointer Arithmetic: You can add an integer offset to the pointer to point at another location Consider the following code: What will happen to the following code? p++; Current value of p = 2000 (address of n) After the p++: p = 2004, not 2,001. Reason? when p is incremented, it will point to the next int. Same for decrement.

Pointer Arithmetic Example: int main() { int n = 33; int *p; p = &n; //p hold the address of n; printf("%x\n", p); p++; //increment p, point to next int return 0; }

Pointer Arithmetic Add or subtract integers to or from pointers: p = p + 9; makes p point to the ninth element of p’s base type Rules: Add/Subtract MUST be perform between a pointer and integer Cannot add (or subtract) pointer and pointer. Cannot add or subtract float or double values to or from pointers

Recall: index starts at 0 Pointer & Array Array & pointer may be used interchangeably: Array name → constant pointer Pointer → array subscripting Consider this fragment: int b[5]; //integer array int *bPtr; //integer pointer bPtr = b; //bPtr points to 1st element of array b //equivalent to bPtr = &b[0]; Purpose Array Pointer Accessing an element → array as pointer → pointer as array b[3] = *(b+3) *(bPtr + 3) = bPtr[3] Address of an element &b[3] bPtr + 3 Integer offset Recall: index starts at 0

Pointer & Array Example: int main() { int number[] = {0, 1, 2, 3, 4}; int *ptr = number; //ptr points to number [0] printf("%d\n", * ptr); ptr ++; //ptr point to next int, number[1] return 0; }

Pointer to Character Array → String Strings are represented as arrays of char values. Exp: Strings stored inside char arrays: (always store ‘\0’ as null terminator) Strings stored as char pointer:

Pointer to Character Array → String Copying character pointer: Determine the output char *p = "Harry"; char *q = p; printf("%s %s\n", p, q); char string1[10]; char *string2 = "hello"; int i = 0; while(string1[i] != '\0' || string2[i] != '\0') { string1[i] = string2[i]; i++; } printf("%s %s\n", string1, string2 );

Pointer to Character Array → String Copying character pointer (function): copy s1 to s2: void copy(const char *s1, char *s2) { int i = 0; while(s1[i] != '\0' || s2[i] != '\0') s1[i] = s2[i]; i++; }

Value vs Reference Parameter Value Parameters: The parameter variables we have looked at so far are called value parameters. Value parameters are separate (new) variables from the ones in main()(or another calling function). Modification of value parameters does not affect the original value. Reference parameters: Reference parameter does not create a new variable, BUT refer to an existing variable Pass the memory address Any change in a reference parameter is actually a change in the variable to which it refers. Syntax: function header: void f2(int *i) //pointer function calling: f2(&val); //pass the variable address Value parameter: Function header: void f1(int i) Function calling: f1(val); Reference parameter: Function header: void f1(int *i) Funciton calling: f1(&val);

Pointer & Function Use pointer to point to the address of variable passed into the function → reference parameter

Reference Parameter Reference parameters are useful in two cases: Change values: change the value of an actual parameter variable in the call. Return multiple value – use combination of return and reference parameter Efficiency: Especially common for passing structs or class objects If no changes are made to the parameter, it should be declared const

Reference Parameter Example 1: Change actual parameter in function calling void f2(int *i); int main() { int val = 10; printf(“Old val in main(): %d \n”, val); f2(&val); printf(“New val in main(): %d \n”, val); return 0; } void f2(int *i) *i = 88; Old val in main(): 10 New val in main(): 88

Reference Parameter Example 2: “return” multiple result #include <math.h> void getSinCos(double dX, double *dSin, double *dCos) { *dSin = sin(dX); *dCos = cos(dX); } int main() double dSin = 0.0; double dCos = 0.0; //getSinCos() will return the sin and cos in dSin and dCos getSinCos(30.0, &dSin, &dCos); printf(“The sin is %lf\n”, dSin); printf(“The cosis %lf\n”, dCos); return 0; }

References Chapter 7: Text File, DCP 2073 C BASIC PROGRAMMING, NIK ISROZAIDI, FSKSM, UTM. (http://www.cheme.utm.my/cheme/images/Undergraduate/SUBJEC T/DCP2073/DCP2073%20NOTES7%20Files.pdf) C++ For Everyone, 2nd edition, Cay Horstmann.