Pointer
Pointer
Syllabus
Pointer
pointer operator
Pointer expression
Declaration of pointer
Initializing pointer
de-referencing
Pointer to pointer
Constant pointer
Array of pointers
Pointer to function.
Pointers
A pointer is a variable whose value is the address of another variable.
Like any variable or constant, you must declare a pointer before you can
work with it. The general form of a pointer variable declaration is −
type *var-name;
Employing the unary operator (&), which yields the address of the
variable, to assign a pointer to a variable's address.
Using the unary operator (*), which gives the variable's value at the
address provided by its argument, one can access the value stored in
an address.
Since the data type knows how many bytes the information is held in, we associate
it with a reference. The size of the data type to which a pointer points is added when
we increment a pointer.
Advantage of pointer
int main() {
int myAge = 43;
printf("%d\n", myAge);
printf("%p\n", &myAge);
return 0;
}
Output
43
0x7ffe5367e044
#include <stdio.h>
One is the original copy and the other is the function copy.
swapx(a, b);
return 0;
}
void swapx(int x, int y)
{
int t;
t = x;
x = y;
y = t;
x = 20 y = 10
In the Caller:
a = 10 b = 20
Call by Reference
Both the actual and formal parameters refer to the same locations.
Any changes made inside the function are actually reflected in the actual
parameters of the caller.
#include <stdio.h>
void swapx(int*, int*);
int main()
{
int a = 10, b = 20;
swapx(&a, &b);
return 0;
}
void swapx(int* x, int* y)
{
int t;
t = *x;
*x = *y;
*y = t;
In this method, the value of each variable in the calling In this method, the address of actual variables in the
function is copied into corresponding dummy variables calling function is copied into the dummy variables of
of the called function. the called function.
With this method, the changes made to the dummy With this method, using addresses we would have access
variables in the called function have no effect on the to the actual variables and hence we would be able to
values of actual variables in the calling function. manipulate them.
In call-by-values, we cannot alter the values of actual In call by reference, we can alter the values of variables
variables through function calls. through function calls.
This method is preferred when we have to pass some This method is preferred when we have to pass a large
small values that should not change. amount of data to the function.
C - Pointer arithmetic
--, +, and –
• To understand pointer arithmetic, let us consider that ptr is an integer
pointer which points to the address 1000.
ptr++
• After the above operation, the ptr will point to the location
1004 because each time ptr is incremented, it will point to the
next integer location which is 4 bytes next to the current
location.
• This operation will move the pointer to the next memory
location without impacting the actual value at the memory
location.
• If ptr points to a character whose address is 1000, then the
above operation will point to the location 1001 because the
#include <stdio.h>
int main ()
{
return 0;
}
Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200
#include <stdio.h>
int main () {
return 0;
}
Address of var[2] = bfedbcd8
Value of var[2] = 200
Address of var[1] = bfedbcd4
Value of var[1] = 100
Address of var[0] = bfedbcd0
Value of var[0] = 10
C - Array of pointers
Before we understand the concept of arrays of pointers, let us consider the following example,
which uses an array of 3 integers −
#include <stdio.h>
const int MAX = 3
int main ()
{
int var[] = {10, 100, 200};
int i;
for (i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, var[i] );
}
return 0;
}
When the above code is compiled and executed, it produces
the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
• There may be a situation when we want to maintain an
array, which can store pointers to an int or char or any
other data type available.
• Following is the declaration of an array of pointers to an
integer −
int main () {
return 0;
}
When the above code is compiled and executed, it produces
the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
#include <stdio.h>
const int MAX = 4;
int main () {
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali"
};
int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
When the above code is compiled and executed, it produces the following
result −
int **var;
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}
When the above code is compiled and executed, it produces
the following result −
return r;
}
1523198053
1187214107
1108300978
430494959
1421301276
930971084
123250484
106932140
1604461820
149169022
*(p + [0]) : 1523198053
*(p + [1]) : 1187214107
*(p + [2]) : 1108300978
*(p + [3]) : 430494959
*(p + [4]) : 1421301276
*(p + [5]) : 930971084
*(p + [6]) : 123250484
*(p + [7]) : 106932140
*(p + [8]) : 1604461820
*(p + [9]) : 149169022