Professional Documents
Culture Documents
Lec 16 PDF
Lec 16 PDF
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
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
. .
. .
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;
? 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.
0 20 144 20
130 144 130 144
144 16
130 144
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
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;
} }
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;
} }
Output: Output:
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;
} }
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;
} }
Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10
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;
} }
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;
} }
Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10
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;
} }
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;
} }
Output: Output:
Before swap function: x = 5, y = 10 Before swap function: x = 5, y = 10
After swap function: x = 10, y = 5
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;
} }
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