0% found this document useful (0 votes)
14 views18 pages

Understanding Variable Addresses and Pointers

Uploaded by

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

Understanding Variable Addresses and Pointers

Uploaded by

i246509
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Programming

Fundamentals
Lecture 9a
Address of a variable
• The address operator (&) returns the memory address of a variable. (for
character variable, address operator & should be cast to (void *).
• Every variable is allocated a section of memory large enough to hold a value of
the variable’s data type.
• On a PC, for instance, it’s common for one byte to be allocated for chars, two
bytes for shorts, four bytes for ints, longs, and floats, and eight bytes for
doubles.
• Each byte of memory has a unique address.
• A variable’s address is the address of the first byte allocated to that variable.
Suppose the following variables are defined in a program:
char letter;
short number;
float amount;
The variable letter is shown at address 1200, number is at address 1201,
and amount is at address 1203. The addresses of the variables shown
are arbitrary values
Address of a variable
• Getting the address of a variable is accomplished with an operator in C++.
• When the address operator (&) is placed in front of a variable name, it returns
the address of that variable.
• Here is an expression that returns the address of the variable amount:
&amount
• And here is a statement that displays the variable’s address on the screen:
cout << &amount;
• Do not confuse the address operator with the & symbol used when defining a
reference variable.
Example
// This program uses the & operator to determine a variable's
2 // address and the size of operator to determine its size.
3 #include <iostream>
4 using namespace std;
5
6 int main()
7{
8 int x = 25;
9
10 cout << "The address of x is " << &x << endl;
11 cout << "The size of x is " << sizeof(x) << " bytes\n";
12 cout << "The value in x is " << x << endl;
13 return 0;
14 }
Pointer variables
• A pointer variable, which often is just called a pointer, is a special variable that
holds a memory address.
• Just as int variables are designed to hold integers, and double variables are
designed to hold floating-point numbers, pointer variables are designed to hold
memory addresses.
• Memory addresses identify specific locations in the computer’s memory.
• Because a pointer variable holds a memory address; it can be used to hold the
location of some other piece of data.
• Pointer Variable “points” to some piece of data that is stored in the computer’s
memory.
• That piece of data is called pointee.
Uses of Pointer variables
Here are some common uses:
• Accessing array elements
• Passing arguments to a function when the function needs to modify the original
argument
• Passing arrays and strings to functions
• Obtaining memory from the system
• Creating data structures such as linked lists
Creating and Using Pointer
Variables
• The definition of a pointer variable looks pretty much like any other definition.
Here is an example:
int *ptr;
• The asterisk in front of the variable name indicates that ptr is a pointer variable.
• The int data type indicates that ptr can be used to hold the address of an integer
variable.
• The definition statement above would read “ptr is a pointer to an int.”
• In this definition, the word int does not mean that ptr is an integer variable. It
means that ptr can hold the address of an integer variable.
• Remember, pointers only hold one kind of value: an address.
• Some programmers prefer to define pointers with the asterisk next to the type
name, rather than the variable name.
Int* ptr;
NULL Pointer
• When we first define a variable, it holds no value (unless we initialize it at the
same time). It may hold a garbage value, but this has no meaning.
• In the case of pointers, a garbage value is the address of something in memory,
but probably not of something that we want.
• When a pointer is declared, it must be initialized.
• It is never a good idea to define a pointer variable without initializing it with a
valid memory address.
• It is a good idea to initialize pointer variables with the special value nullptr if a
variable is not available.
int *ptr = nullptr;
• In C++, the nullptr key word is introduced to represent the address 0
• When a pointer is set to the address 0, it is referred to as a null pointer because
it points to “nothing.”
Example
// This program stores the address of a variable in a pointer.
2 #include <iostream>
3 using namespace std;
4
5 int main()
6{
7 int x = 25; // int variable
8 int *ptr = nullptr; // Pointer variable, can point to an int
9
10 ptr = &x; // Store the address of x in ptr
11 cout << "The value in x is " << x << endl;
12 cout << "The address of x is " << ptr << endl;
13 return 0;
14 }
Dereferencing a Pointer
• The real benefit of pointers is that they allow you to indirectly access and modify
the variable being pointed to.
• This is done with the indirection (sometimes dereference) operator, which is an
asterisk (*).
• When the indirection operator is placed in front of a pointer variable name, it
dereferences the pointer.
• When you are working with a dereferenced pointer, you are actually working
with the value the pointer is pointing to.
Example
// This program demonstrates the use of the indirection operator.
2 #include <iostream>
3 using namespace std;
4
5 int main()
6{
7 int x = 25; // int variable
8 int *ptr = nullptr; // Pointer variable, can point to an int
9
10 ptr = &x; // Store the address of x in ptr
11
12 // Use both x and ptr to display the value in x.
13 cout << "Here is the value in x, printed twice:\n";
14 cout << x << endl; // Displays the contents of x
15 cout << *ptr << endl; // Displays the contents of x
16
17 // Assign 100 to the location pointed to by ptr. This
18 // will actually assign 100 to x.
19 *ptr = 100;
// Use both x and ptr to display the value in x.
22 cout << "Once again, here is the value in x:\n";
23 cout << x << endl; // Displays the contents of x
24 cout << *ptr << endl; // Displays the contents of x
25 return 0;
26 }
Example
// This program demonstrates a pointer variable referencing
2 // different variables.
3 #include <iostream>
4 using namespace std;
int main()
7{
8 int x = 25, y = 50, z = 75; // Three int variables
9 int *ptr = nullptr; // Pointer variable
11 // Display the contents of x, y, and z.
12 cout << "Here are the values of x, y, and z:\n";
13 cout << x << " " << y << " " << z << endl;
15 // Use the pointer to manipulate x, y, and z.
17 ptr = &x; // Store the address of x in ptr.
18 *ptr += 100; // Add 100 to the value in x.
20 ptr = &y; // Store the address of y in ptr.
21 *ptr += 100; // Add 100 to the value in y.
23 ptr = &z; // Store the address of z in ptr.
24 *ptr += 100; // Add 100 to the value in z.
26 // Display the contents of x, y, and z.
27 cout << "Once again, here are the values of x, y, and z:\n";
28 cout << x << " " << y << " " << z << endl;
29 return 0;
30 }
Pointer to void
• The address that you put in a pointer must be the same type as the pointer.
• You can’t assign the address of a float variable to a pointer to int, for example:
float flovar = 98.6;
int* ptrint = &flovar; //ERROR: can’t assign float* to int*
• However, there is an exception to this. There is a sort of general-purpose pointer
that can point to any data type.
• This is called a pointer to void, and is defined like this:
void* ptr; //ptr can point to any data type
• Such pointers have certain specialized uses, such as passing pointers to functions
that operate independently of the data type pointed to.
Example
// [Link]
// pointers to type void
#include <iostream>
using namespace std;
int main()
{
int intvar; //integer variable
float flovar; //float variable
int* ptrint; //define pointer to int
float* ptrflo; //define pointer to float
void* ptrvoid; //define pointer to void
ptrint = &intvar; //ok, int* to int*
// ptrint = &flovar; //error, float* to int*
// ptrflo = &intvar; //error, int* to float*
ptrflo = &flovar; //ok, float* to float*
ptrvoid = &intvar; //ok, int* to void*
ptrvoid = &flovar; //ok, float* to void*
return 0;
}

You might also like