You are on page 1of 10

What is a Pointer?

• So far, we have seen that a variable is used to store a value.


• Variables allow the programmer to directly manipulate the data in
memory.
• A pointer variable, however, does not store a value but store the
address of the memory space which contain the value i.e. it
directly points to a specific memory address.
• Why would we want to use pointers?

Lecture 16 • To call a function by reference so that the data passed to the function
can be changed inside the function.
• To create a dynamic data structure which can grow larger or smaller
Pointers as necessary.
CSE115: Programming Language I

Variable Declaration Pointers


• A variable declaration such as, • A pointer is a variable that contains the address of another variable
• char letter = ‘A’; causes the compiler to allocate a memory location for the variable letter
and store in it the integer value 20.
• This address of the memory location is available to our program during the run time.
• The computer uses this address to access its content.
address content
0x00000000
0x00000001
.
address content
0x00000000 .

0x00000001 .
letter 0x180A96e8 65
letter . ptr letter
. 0x180A96e9
65 0x180A96e8 65 ptr 0x180A96f0
.
0x180A96e8 letter 0x180A96e8 65 0x180A96f0 0x180A96e8 0x180A96f1
0x180A96e8
The name letter is associated 0x180A96e9 The variable ptr contains the 0x180A96f2

with the address 0x180A96e8 0x180A96f0 address of letter 0x180A96f3

. .

. .
Pointers Pointer Declaration
• A pointer is a variable that contains the address of another variable • General Format:
• We say that a pointer points/references another variable data_type *pointer_name;

• A pointer declaration such as,


address content int *numberPtr;
0x00000000 • declares numberptr as a variable that points to an integer variable.
0x00000001 Its content is a memory address.
.
. • The * indicates that the variable being declared is a pointer variable instead of a
. normal variable.
letter 0x180A96e8 65
ptr letter
0x180A96e9
0x180A96e8 65 ptr 0x180A96f0
0x180A96f0 0x180A96e8 0x180A96f1
0x180A96e8
The variable ptr contains the 0x180A96f2

address of letter 0x180A96f3


.
.

Pointer Declaration Pointer Initialization


• Consider the following declaration • To prevent the pointer from pointing to a random memory address, it is
int *numberPtr, number = 20; advisable that the pointer is initialized to NULL (the value 0) or an address
before being used.
• In this case, two memory address have been reserved, associated
with the names numberPtr and number. • A pointer with the value NULL, points to nothing.
• The value in variable number is of type integer, and the value in • Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL
variable numberPtr is an address for another memory. is preferred.
int *numberPtr = NULL, number = 20;

numberPtr number numberPtr number

? 20 0 20
130 144 130 144
Pointer Operator (& and *) Pointer Operator (& and *)
• When a pointer is created, it does not point to any valid memory address. • When a pointer is created, it does not point to any valid memory address.
Therefore, we need to assign a variable’s address to it Therefore, we need to assign a variable’s address to it
• using the & operator (referencing operator/ address-of operator). • using the & operator (referencing operator/ address-of operator).
• Look at this example: • Look at this example:
int *numberPtr, number = 20; int *numberPtr, number = 20;
numberPtr = NULL; numberPtr = NULL;
numberPtr = &number;
• The statement numberPtr = &number assigns the address of the variable
number to a pointer variable numberPtr. Variable numberPtr is then said
as to “point to” variable number.

numberPtr number numberPtr number

0 20 144 20
130 144 130 144

Pointer Operator (& and *) Pointer Operator (& and *)


• After a pointer is assigned a particular address, the value at the pointed address
numberPtr number address content can be accessed/modified
0 • using the * operator (dereferencing operator/ value-at operator).
144 20 1
• Look at this example:
130 144 .
int *numberPtr, number = 20;
. numberPtr = NULL;
. numberPtr = &number;
numberPtr 130
131
144
132
133
.
.
number 144
145 numberPtr number
20
146
144 20
147
130 144
.
.
Pointer Operator (& and *) Example: & and *
• After a pointer is assigned a particular address, the value at the pointed address #include <stdio.h>
can be accessed/modified void main(void)
{
• using the * operator (dereferencing operator/ value-at operator). int var = 10;
int *ptrvar = &var;
• Look at this example:
int *numberPtr, number = 20; printf("The address of the variable var is: %08x\n", &var);
numberPtr = NULL; printf("The value of the pointer ptrvar is: %08x\n", ptrvar);
numberPtr = &number; printf("Both values are the same\n");
*numberPtr = 16; printf("The value of the variable var is: %d\n", var);
printf(“number = %d”, number); printf("The value of *ptrvar is: %d\n", *ptrvar);
printf("Both values are the same\n");
• The statement *numberPtr = 16 changes the content at the address 144
from 20 to 16. printf("The address of the value pointed by ptrvar is: %d\n",
&*ptrvar);
printf("The value inside the address of ptrvar is: %d\n",
*&ptrvar);
printf("Both values are the same\n");
numberPtr number }

144 16
130 144

Example: & and * Pointer to Pointer


/*Sample Output */ • A pointer can also be made to point to a pointer variable.
The address of the variable var is: 1245052 The declaration is a bit different. For example,
The value of the pointer ptrvar is: 1245052 int a;
Both values are the same
int *p; //Pointer to integer
The value of the variable var is: 10 int **q; //Pointer to pointer to integer
The value of *ptrvar is: 10
Both values are the same
p = &a;
q = &p;
The address of the value pointed by ptrvar is: 1245052 *p = 10; //Assigns 10 to variable a
The value inside the address of ptrvar is: 1245052
Both values are the same **q = 20; //Assigns 20 to variable a

Press any key to continue


Parameter Passing by Pointer Parameter Passing by Pointer
• A function may return multiple values by declaring their • When a pointer is passed to a function, we are actually
formal parameters (passing value) as pointers variables. passing the address of a variable to the function.
• This way of passing the argument is known as call by reference • Since we have the address, we can directly manipulate the
• When the value referenced by the pointer is changed inside data in the address.
the function, the value in the actual variable will also change. • In the case where a non-pointer variable is passed, the
• Therefore, we can pass the result of the function through the function will create another space in memory to hold the
function argument without having to use the return value locally while the program is inside the function.
statement. Therefore, any change to the variable inside the function will
not change the actual value of the variable.

Parameter Passing by Pointer Parameter Passing by Pointer


#include <stdio.h> #include <stdio.h>

void swap(int a, int b) void swap(int a, int b)


{ {
int temp; int temp;
temp = a; temp = a;
a = b; a = b;
b = temp; b = temp;
} }

int main(void) int main(void)


{ {
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(x,y); swap(x,y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output:
Before swap function: x = 5, y = 10
After swap function: x = 5, y = 10
Parameter Passing by Pointer Parameter Passing by Pointer
#include <stdio.h> • Declare the parameters of swap as pointer variables so that they can
contain addresses.
void swap(int a, int b)
{ void swap(int *addr1, int * addr2)
int temp;
temp = a;
• We will place the addresses of x and y into addr1 and addr2,
a = b; respectively.
b = temp; swap(&x, &y);
} Local variables (gets destroyed
after function ends, no effect on
int main(void) x and y inside main)
{
int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y);
swap(x,y);
printf("After swap function: x = %d, y = %d",x,y);
return 0;
}

Output:
Before swap function: x = 5, y = 10
After swap function: x = 5, y = 10

Parameter Passing by Pointer Parameter Passing by Pointer


#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) void swap(int *addr1, int *addr2)
{ {
int temp; int temp;
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ {
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output:
Parameter Passing by Pointer Parameter Passing by Pointer
#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) void swap(int *addr1, int *addr2)
{ {
int temp; int temp;
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ x 5 y 10 { x 5 y 10
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output: Output:

Parameter Passing by Pointer Parameter Passing by Pointer


#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) void swap(int *addr1, int *addr2)
{ {
int temp; int temp;
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ x 5 y 10 { x 5 y 10
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10
Parameter Passing by Pointer Parameter Passing by Pointer
#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) void swap(int *addr1, int *addr2) temp addr1 addr2
{ {
int temp; int temp; ?
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ x 5 y 10 { x 5 y 10
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10

Parameter Passing by Pointer Parameter Passing by Pointer


#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) temp addr1 addr2 void swap(int *addr1, int *addr2) temp addr1 addr2
{ {
int temp; ? int temp; 5
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ x 5 y 10 { x 5 y 10
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10
Parameter Passing by Pointer Parameter Passing by Pointer
#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) temp addr1 addr2 void swap(int *addr1, int *addr2) temp addr1 addr2
{ {
int temp; 5 int temp; 5
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ x 5 y 10 { x 10 y 10
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10

Parameter Passing by Pointer Parameter Passing by Pointer


#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) temp addr1 addr2 void swap(int *addr1, int *addr2) temp addr1 addr2
{ {
int temp; 5 int temp; 5
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ x 10 y 10 { x 10 y 5
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10
Parameter Passing by Pointer Parameter Passing by Pointer
#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) void swap(int *addr1, int *addr2)
{ {
int temp; int temp;
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ x 10 y 5 { x 10 y 5
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10
After swap function: x = 10, y = 5

Parameter Passing by Pointer Parameter Passing by Pointer


#include <stdio.h> #include <stdio.h>

void swap(int *addr1, int *addr2) void swap(int *addr1, int *addr2)
{ {
int temp; int temp;
temp = *addr1; temp = *addr1;
*addr1 = *addr2; *addr1 = *addr2;
*addr2 = temp; *addr2 = temp;
} }

int main(void) int main(void)


{ x 10 y 5 {
int x = 5, y = 10; int x = 5, y = 10;
printf("Before swap function: x = %d, y = %d\n",x,y); printf("Before swap function: x = %d, y = %d\n",x,y);
swap(&x,&y); swap(&x,&y);
printf("After swap function: x = %d, y = %d",x,y); printf("After swap function: x = %d, y = %d",x,y);
return 0; return 0;
} }

Output:
Output:
Before swap function: x = 5, y = 10
Before swap function: x = 5, y = 10
After swap function: x = 10, y = 5
After swap function: x = 10, y = 5

You might also like