Download presentation
Presentation is loading. Please wait.
1
Chapter 8 Arrays, Strings and Pointers
8b: Pointers
2
Goals: Pointers & pointer operators Functions: Reference Parameter
Pointers & Arrays Arrays of strings
3
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)
4
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; }
5
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
6
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:
7
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.
8
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
9
Introduction - Pointers
Pointer Initialization:
10
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
11
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;
12
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; }
13
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
14
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; }
15
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
16
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; }
17
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.
18
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; }
19
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
20
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
21
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; }
22
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:
23
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 );
24
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++; }
25
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);
26
Pointer & Function Use pointer to point to the address of variable passed into the function → reference parameter
27
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
28
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
29
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; }
30
References Chapter 7: Text File, DCP 2073 C BASIC PROGRAMMING, NIK ISROZAIDI, FSKSM, UTM. ( T/DCP2073/DCP2073%20NOTES7%20Files.pdf) C++ For Everyone, 2nd edition, Cay Horstmann.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.