0% found this document useful (0 votes)
22 views

FEE 132 Computer Labs

The document discusses pointers in C++, explaining that pointers are variables that contain the address of other variables and allow indirect referencing of values through dereferencing. It covers how to define and initialize pointers, use the address and dereference operators, pass pointers to functions by reference, and how pointers relate to arrays and strings. Examples are provided of pointer usage in swapping variable values and implementing string comparison functions.

Uploaded by

Simon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

FEE 132 Computer Labs

The document discusses pointers in C++, explaining that pointers are variables that contain the address of other variables and allow indirect referencing of values through dereferencing. It covers how to define and initialize pointers, use the address and dereference operators, pass pointers to functions by reference, and how pointers relate to arrays and strings. Examples are provided of pointer usage in swapping variable values and implementing string comparison functions.

Uploaded by

Simon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

FEE 132 Computer Labs

Recap of FEE 131 Computer Labs


Introduction to C++ Programming
• Variables and data types
• Flow control: the 3 constructs
• Loops
• Functions
• Arrays
• Pointers
POINTERS
Swapping two variables’ values
#include <stdio.h>

int main(void)
{
int x = 1; int y = 2;

cout << “x is “ << x << “and y is “ << y;

int tmp = x;
x = y;
y = tmp;

cout << “x is “ << x << “and y is “ << y;


}
#include <stdio.h>
void mySwap(int a, int b); //what do we call
this?

int main(void)
{
int x = 1; int y = 2;

cout << “x is “ << x << “and y is “ << y;

mySwap(x, y);

cout << “x is “ << x << “and y is “ << y;


}
swap
void mySwap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
Pointers
• A pointer is a variable that contains the address of a variable
• They usually lead to more compact and efficient code than can be
obtained in other ways
• Pointers and arrays are closely related; strings as well
• Important when it comes to handling memory
• microcontroller & embedded programming
Pointers
• Ordinary Variables
• Directly contains a specific value
• A variable name directly references a value

• Pointer
• contains an address of a variable that contains a specific value
• A pointer indirectly references a value
• Referencing a value through a pointer is called indirection
Pointers
Pointers
• Pointers, like all variables, must be defined before they can be used

int *countPtr, count;

• “countPtr is a pointer to int” or “countPtr points to an object of type


int.”
• Pointers should be initialized either when they’re defined or in an
assignment statement. A pointer may be initialized to NULL, 0 or an address
Pointers
• The &, or address operator, is a unary operator that returns the
address of its operand

int y = 5;
int *yPtr;
yPtr = &y;
Pointers
Pointers
• The unary * operator, commonly referred to as the indirection
operator or dereferencing operator, returns the value of the object
to which its operand (i.e., a pointer) points

cout << *yPtr ;

• Using * in this manner is called dereferencing a pointer


• What would the following line produce:

cout << yPtr ;


What is the value of e, f?
int a = 10;
int b = 30;

int* c = &a;
int* d = &b;

int e = a + b;
int f = (*c) + (*d);
What is the final value of a, b, c, d?
int a = 10;
int b = 30;

int* c = &a;
int* d = &b;

int tmp = *c;


*c = *d;
*d = tmp;
The real swap
void mySwap(int* px, int* py)
{
int tmp = *px;
*px = *py;
*py = tmp;
}
Calling the function that takes pointer
arguments

mySwap(&x, &y);

This gets the address of x and y and supplies those as inputs to the
function mySwap
Passing Arguments by Reference Vs Passing by
Value
int a = 10; int b = 30;

mySwap(&a, &b); // by reference

Vs

mySwap(a, b); // by value


Passing Arguments by Reference Vs Passing by
Value

• When arguments are passed to functions by value, there is no direct


way for the called function to alter a variable in the calling function
• Pointer arguments enable a function to access and change objects in
the function that called it
• Terminology: call-by-value and call-by-reference
Pointers and Arrays
• Any operation that can be achieved by array subscripting can also be
done with pointers. The pointer version will in general be faster but, at
least to the uninitiated, somewhat harder to understand
int a[10];
int *pa;
pa = &a[0];
x = *pa; // copies the contents of a[0] into x

• If pa points to a particular element of an array, then by definition pa+1


points to the next element, pa+i points i elements after pa, and pa-i
points i elements before.
• if pa points to a[0], *(pa+1) refers to the contents of a[1], pa+i is the
address of a[i], and *(pa+i) is the contents of a[i].
Array indices Vs Pointer offsets
• The value of a variable or expression of type array is the
address of element zero of the array

pa = &a[0]; // pa and a have identical values

pa = a; // Same as the statement above

• a[i] can also be written as *(a+i)


Arrays and Pointers
• &a[i] and a+i are also identical
• pa[i] is identical to *(pa+i)

• An array-and-index expression is equivalent to one written as a


pointer and offset
• Arrays passed as arguments to functions are always passed by
reference
Strings are just character arrays
string myString_1 = “Today is Tuesday”;

char *myString_2 = “Today is Tuesday”;

cout << myString_1[2];


cout << myString_2[2];
// equivalent to:
cout << *(myString_1+2) ;
cout << *(myString_2+2) ;
Strings are terminated by \0
#include <iostream>

int main(void)
{
char * s = “hello”;
char * t = “hello”;

// try (and fail) to compare strings NB: Comparing strings in this way
if (s == t) doesn’t work! Try it out!
{
cout << "You typed the same thing!\n";
}
else
{
cout << "You typed different things!\n";
}
}
#include <iostream>
#include<string>
int main(void)
{
string s = “hello”;
string t = “hello”;

// try to compare strings


if (s != NULL && t != NULL) NB: This is how to compare strings
{
if (strcmp(s, t) == 0)
i.e. using the function strcmp
{
cout << "You typed the same thing!\n";
}
else
{
printf("You typed different things!\n");
}
}
}
string
• what is the implementation of strlen ???

• What is the implementation of strcmp ???


• strcmp(char *a, char *b)

• Copying a string: a little more complicated than just using the


assignment operator
string
Read more about strings at
http://www.tutorialspoint.com/cplusplus/cpp_strings.htm

and, read more about pointers at


http://www.cplusplus.com/doc/tutorial/pointers/

You might also like