0% found this document useful (0 votes)
29 views15 pages

Chapter Three-Ponters

Chapter Three discusses the concept of pointers in programming, including their declaration, initialization, and operations such as dereferencing and arithmetic. It explains the relationship between pointers and arrays, as well as their use in function calling by reference. The chapter also highlights the advantages of using pointers, such as dynamic memory management and pass-by-reference semantics.

Uploaded by

Tucho Yadeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views15 pages

Chapter Three-Ponters

Chapter Three discusses the concept of pointers in programming, including their declaration, initialization, and operations such as dereferencing and arithmetic. It explains the relationship between pointers and arrays, as well as their use in function calling by reference. The chapter also highlights the advantages of using pointers, such as dynamic memory management and pass-by-reference semantics.

Uploaded by

Tucho Yadeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter Three

Pointers
Injibara University
2015 E.C

1
Outline
 Basic concept of pointers

 Pointer variables and declaration

 Pointer expressions, operation and arithmetic

 Strings and pointers

 Relationship between pointers and arrays

 Revisiting function calling by reference (using pointers) (Reading assignment)

2
Basic concept of pointers
 A variable whose value is the address of another variable is known as pointer.

 If p is a pointer to variable x, then p holds the address of x.

 The address-of operator (&), can be used to get the memory address of an object.

 Accessing the object to which a pointer points is called dereferencing the pointer.

 The "*" operator, when it appears in an expression, dereferences the pointer to its right.

 Declaration of pointer variable follows the following syntax:


type *valid_identifier; example: int *p;

 Pointers initialization: Initialized to 0, NULL, or an address, 0 or NULL points to nothing.

3
Cont.
 Pointer variables are declared using the * (dereference operator).

 Example: int * p; // p is a variable with type "Pointer to int"

 Generally <Type> * x; declares x to be a pointer to data of type <Type>.

 Pointer values can be initialized to "point" to other variables

int x = 5;

int *p = &x;

 This means "p is a pointer to int. Its value is the address where x is stored"

4
How to read pointer
 Reading an address directly is usually useless.

 What we want to do is tell the program to "dereference" a pointer, that is, read the data the
pointer "points to".

cout << p << endl;

cout << *p << endl;

 The second line means "the value written in the address stored in p“.

5
Dereferencing a pointer
 Generally, the * operator can be applied to a pointer to type T.

 The result is an expression of type T

 It is also an lvalue. Example:

int x = 5;

int *p = &x;

*p = 6;

cout << x << endl; // will print out 6

6
Pointers operators
 (indirection/dereferencing operator)
Returns the value of what its operand points to
*yPtr returns y (because yPtr points to y).
* can be used to assign a value to a location in memory
*yptr = 7; // changes y to 7

 Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are
inverses, cancel each other out
*&myVar == myVar and
&*yPtr == yPtr

7
Pointer expression, operation and arithmetic
int x, y, *p;

p = &x; /* p gets the address of x */

y = *p; /* y gets the value pointed to by p*/

y = *(&x); /* same as y = x */

 Unary operators associate right to left: y = *&x; /* same as y = *(&x) */

 Unary operators bind more tightly than binary ones


 y = *p + 1; /* same as y = (*p) + 1; */

 y = *p++; /* same as y = *(p++); */

8
Cont.
 Arithmetic operations on a pointer is the same as a numeric value.

 The ++, --, +, and -are the main four types of arithmetic operators applied to the pointers.

 Problem: write a program in C++ that finds the location of integer array with five elements.

 Comparing pointers is possible using relational operators like ==,, and >.

 P1 and P2 can be meaningfully compared if they both point to variables that are connected to
one another, such as the items of the same array.

9
Pointers and strings
 Pointers are declared to point to a specific type and can point to objects of that type only.

 A pointer variable contains a memory address as its value.

 A pointer
 int* intptr; // intptr can store the address of an integer variable

 double* doubleptr; // doubleptr can store the address of a double variable

 char* charptr; // charptr can store the address of a character variable, or pointer for short, points to the
object whose address it stores.

10
Pointers and arrays
 Array names are constant pointers
int a[10], *p, i;

p = a; /* p points to a[0] */

a++; /* Illegal; can’t change a constant */

p++; /* Legal; p is a variable */

 Subscripting is defined in terms of pointers

a[i], *(a+i), i[a] /* Legal and the same */

&a[i], a+i /* Legal and the same */

p = &a[0] /* &*(a+0) &*a a */

11
Cont.
 It does not matter where you put the asterisk as long as it is between the type and the variable
name these three are equivalent: int* intptr; int * intptr; int *intptr;

 In general, if T is some existing type, T* is the type of a variable that can store the address of
objects of type T, whether it is a simple scalar variable such as int, double, or char, or a more
structured variable such as a structure or a class:

 struct Point { int x; int y; };

 Point* Pointptr;

12
Cont.
 If the symbol "&" appears in a declaration, it is declaring that the variable to its right is a
reference variable, whereas if it appears in an expression that is not a declaration, it is the
address-of operator and its value is the address of the variable to its right.

 int x = 972384; int y = 100; int &ref = x; // a declaration, so & is the reference operator int
*p = &x; // appears on right of =, so it is in an // expression and is address-of operator; p
stores address of x p = &y; // address-of operator; p stores the address of y int &z = y; //
reference operator; z is an alias for y

13
The relationship between array and pioneer
 Arrays and pointers closely related.

 Array name is the same as constant pointer.

 Pointers can do array subscripting operations.

 Having declared an array b[ 5 ] and a pointer bPtr, bPtr is equal to b, bptr == b.

 bptr is equal to the address of the first element of b, bptr == &b[0].

14
Advantage of pointers
 Pointers are useful in C++ for several reasons

 To allow pass-by-ref semantics (can also be done with references)

 To allow dynamic memory management.

 Pass-by-ref with pointers Call by ref

15

You might also like