Lec 20 Pointers in C
Lec 20 Pointers in C
•Pointer?
• Declaring Pointers
• Dereferencing Operator
• Initializing Pointers
• NULL Pointer
• Increment / Decrementing a Pointer
•Pointers and Functions
• Passing Pointers to Functions
• Functions returning Pointer variables
• Safe Way to Return a valid Pointer 2
What is a Pointer?
• is a special kind of variable in which we store memory addresses
(Instead of data values).
• Suppose you want to give a prize to the winner.
• In such case you have two options:
type_of_stored_data *pointer_variable_name;
For example,
Char *chName;
int *nTypeOfCar;
float *fValue;
type_of_stored_data is any valid pointer base type such as char, int, float or
other valid C derived types such as array and struct.
4
Declaring Pointers
Pointer Syntax
No space
int *myptr; // Read Right to Left
//myptr is a pointer to an integer
Similarly
int y;
int *x; // Pointing to double ; 0x00128940
x=&y;
Char *C; // Pointing to char
5
Assigning addresses to Pointers
Example:
int *ptr; // Declaration, Pointer is a whole number big enough to store memory address
int x;
x = 10;
Now in order to read the value in that memory location, we use “star operator” that is
6
Dereferencing Operator *
• *Ptr is read as the value of whatever ptr points to
Z = *ptr * 2; // 20 will be stored in Z
Initializing Pointers:
Giving starting value to a Pointer that is
Ptr = &var;
Ptr = 0;
Ptr = NULL;
Int *p = NULL;
Char *p = NULL;
8
Get Value of Thing Pointed by Pointers
Example:
int *pc, c;
c = 5;
pc = &c;
9
Changing Value Pointed by Pointers
Example:
int *pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
Note: We have assigned the address of c to the pc pointer.
Then, we changed the value of c to 1. Since pc and the address of c
is the same, *pc gives us 1. 10
Declaring Pointers
int *ptr1; // one pointers on one line.
int c, *pc;
When you increment a variable, you usually add 1 to it. For example,
the value of i in the following code will be 1:
int i = 0;
i++;
Because a pointer points to an address (which is also a numeric value), you can also
increment a pointer. However, you are incrementing by address value instead of integer value.
In most C compilers, an integer is stored as 4 bytes. Therefore, if your integer pointer has a
value of 62fe30, incrementing the pointer will result in a new address of 62fe34.
Note that memory addresses are stored as hexadecimal values. The decimal value of 62fe34 is
6487604. 13
Incrementing the Address of Pointer
The following code loops through an array, incrementing the pointer along the way. In the for
loop, it displays the address (ptr) and the value (*ptr):
Because a pointer points to another value (e.g., an address in memory), it is also useful for
array processing. We can indicate the bucket of an array by using its index; but we can also
use pointers. For example, look at the following array and how the incrementing works.
#include <stdio.h>
int main(void) Output:
{
int scores[5] = {100, 235, 275, 50, 100};
Value stored in pointer after increment is: 235
int *ptr = NULL;
ptr = &scores;
In this case we use increment operator before the pointer variable like ++ptr, and use the asterisk (*) to point
to the value. When you run this, it will indicate the second index, because we have incremented by one.15
Examples
Example 1.
int *pc, c; Explanation 1.
c = 5; We have assigned the address of c to the pc pointer.
pc = &c;
*pc = 1; Then, we changed *pc to 1 using *pc = 1;. Since pc and
printf("%d", *pc); // Ouptut: 1 the address of c is the same, c will be equal to 1.
printf("%d", c); // Output: 1
Example 2.
int *pc, c, d;
c = 5; Explanation 2.
d = -15; Initially, the address of c is assigned to the pc pointer
pc = &c; using pc = &c;. Since c is 5, *pc gives us 5.
printf("%d", *pc); // Output: 5
pc = &d; Then, the address of d is assigned to the pc pointer using
printf("%d", *pc); // Ouptut: -15 pc = &d;. Since d is -15, *pc gives us -15.
16
Example
#include <stdio.h>
int main()
{
int *pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
17
Example
*pc = 2;
Output
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2 Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
18
Passing Pointer to a Function (Revision)
19
Example Time: Swapping two numbers using Pointer
#include <stdio.h>
void swap(int *a, int *b);
int main()
{
int m = 10, n = 20;
printf("m = %d\n", m);
printf("n = %d\n\n", n);
swap(&m, &n);//passing address of m and n to the swap
function
printf("After Swapping:\n\n");
printf("m = %d\n", m);
printf("n = %d", n);
return 0;
} 20
Example Time: Swapping two numbers using Pointer
/*
pointer 'a' and 'b' holds and
points to the address of 'm' and 'n‘*/
A function can also return a pointer to the calling function. In this case you must be careful,
because local variables of function doesn't live outside the function. They have scope only
inside the function. Hence if you return a pointer connected to a local variable, that pointer
will be pointing to nothing when the function ends.
#include <stdio.h>
int *larger(int*, int*);
void main()
{
int a = 15;
int b = 92;
int *p;
p = larger(&a, &b);
22
Functions returning Pointer variables
printf("%d is larger",*p);
}
int *larger(int *x, int *y)
{
if(*x > *y)
return x;
else
return y;
}
OutPut
92 is larger
23
Safe Way to Return a valid Pointer
• Either use arguments with functions, Because argument passed to the function are declared
inside the calling function, hence they will live outside the function as well.
OR,
• use static local variables inside the function and return them. As static variable has lifetime
until the main() exists Therefore they will be available through out the program.
24
Summery
• Pointer?
• Declaring Pointers
• Dereferencing Operator
• Initializing Pointers
• NULL Pointer
• Increment / Decrementing a Pointer
• Pointers and Functions
• Pointer to a function
• Declaring Pointer to a function
• Assigning address of function to a Pointer
• Calling mechanism of pointer to function
• Passing Pointers to Functions
• Functions returning Pointer variables
• Safe Way to Return a valid Pointer
25
26