Pointers
Pointers
Pointer variables in C
Before you get into the concept of pointers, let's first get familiar with addresses
in C.
If you have a variable var in your program, &var will give you its address in the
memory, where & is commonly called the reference operator.
You must have seen this notation while using scanf() function. It was used in the
function to store the user inputted value in the address of var, I know you have
N
SO
already seen the magic “&”.
EL
N
scanf("%d", &var); // values are actually stored in memory locations of variables
LY
#include <stdio.h> SA
int main()
ZA
{
BI
int var = 5;
U
AS
}
ed
ar
Output
ep
pr
Note: You may obtain different values of address while using this code.
In above source code, value 5 is stored in the memory location 0x7ffcac23e854. var
is just the name given to that location.
In C, you can create a special variable that stores the address (rather than the
value). This variable is called a : “pointer variable” or simply a: ”pointer”.
As discussed, & is called a reference operator. It gives you the address of a variable.
Likewise, there is another operator that gets you the value from the address, it is
called a dereference operator, which is *.
Below example clearly demonstrates the use of pointers, reference operator and
dereference operator.
N
SO
Note: The * sign when declaring a pointer is not a dereference operator. It is just a
EL
similar notation that creates a pointer.
N
LY
Example: How Pointer Works? SA
ZA
#include <stdio.h>
BI
U
int main()
AS
IR
{
by
int * pc, c;
ed
ar
ep
c = 22;
pr
pc = &c;
printf("Address of c: %p\n", pc);
printf("Content pointed by pc: %d\n\n", *pc);
c = 11;
printf("Address of c: %p\n", pc);
printf("Content pointed by pc: %d\n\n", *pc);
N
SO
EL
N
LY
SA
ZA
1. int* pc, c;
IR
Here, a pointer pc and a normal variable c, both of type int, is created. Since
by
garbage value.
pr
2. c = 22;
3. pc = &c;
This assigns the address of variable c to the pointer pc.
You see the value of pc is same as the address of c and the value pointed by pc
is 22 as well.
4. c = 11;
5. *pc = 2;
This changes the value at the memory location pointed by pointer pc to 2. Since
N
SO
pc always points to c, the value of c is also changed to 2. This is now more
EL
Interesting! How a pointer can easily modify the value of the variable it points
N
LY
to.
SA
Call a function by value and Call by reference in
ZA
There are two methods to pass the data into the function in C language, i.e.,
BI
U
Call by value
by
ed
ar
- In the call by value method, the value of the actual parameters is copied into
ep
the formal parameters. In other words, we can say that the value of the
pr
Call by reference
When we call a function by passing the addresses of actual parameters then this
way of calling the function is known as call by reference. In call by reference,
the operation performed on formal parameters affects the value of actual
parameters because all the operations performed on the value stored in the
address of actual parameters. It may sound confusing first but the following
example would clear your doubts.
Example 1:
#include <stdio.h>
N
void increment(int *var)
SO
EL
N
{
LY
SA
/* Although we are performing the increment on variable
ZA
BI
U
*/
*var = *var+1;
int main()
int num=20;
/* This way of calling the function is known as call by
*/
increment(&num);
N
SO
return 0;
EL
N
LY
} SA
ZA
When the code above is compiled and executed, it produces the following result:
BI
U
AS
IR
by
ed
#include<stdio.h>
int tempnum ;
tempnum = *var1 ;
*var1 = *var2 ;
*var2 = tempnum ;
}
int main( )
printf("Before swapping:");
N
SO
printf("\nnum2 value is %d", num2);
EL
N
LY
/*calling swap function*/ SA
ZA
printf("\nAfter swapping:");
IR
by
ed
return 0;
int a = 5;
int *ptr = NULL;
N
SO
EL
ptr = &a; // address to pointer assignment
N
LY
This assigns the value of the address of a to ptr. For example, if a is stored at
SA
memory location of 0x8130 then the value of ptr will be 0x8130 after the
ZA
*ptr = 8;
AS
IR
by
ed
ar
ep
pr
This means take the contents of ptr (which is 0x8130), "locate" that address in
memory and set its value to 8. If a is later accessed again, its new value will be
8. Which means :”A pointer can change the value of a variable!”
This example may be clearer if memory is examined directly. Assume that a is located
at address 0x8130 in memory and ptr at 0x8134; also assume this is a 32-bit
machine such that an int is 32-bits wide. The following is what would be in memory
after the following code snippet is executed:
int a = 5;
int *ptr = NULL;
Address Contents
N
0x8130 0x00000005
SO
0x8134 0x00000000
EL
N
LY
SA
(The NULL pointer shown here is 0x00000000.) By assigning the address of a to ptr:
ZA
ptr = &a;
BI
Address Contents
IR
by
0x8130 0x00000005
ed
0x8134 0x00008130
ar
ep
*ptr = 8;
the computer will take the contents of ptr (which is 0x8130), 'locate' that address,
and assign 8 to that location yielding the following memory:
Address Contents
0x8130 0x00000008
0x8134 0x00008130
Clearly, accessing a will yield the value of 8 because the previous instruction
modified the contents of a by way of the pointer ptr.
Common mistakes when working with pointers
Suppose, you want a pointer pc to point to the address of c. Then,
int c, *pc;
// Wrong! pc is address whereas,
// c
is not
an
addre
ss. pc
= c;
// Wrong! *pc is the value pointed by address whereas,
// &c is an address.
N
SO
pc = &c;
EL
// Correct! pc is an address and,
N
LY
// &c is SA
also an
ZA
address
BI
U
. ptr =
AS
IR
&a;
by
*pc = c;
pr
#include <stdio.h>
int main(void) {
char name[] = "Harry Potter"; // A string initialization as a array of
characters
char *namePtr;
namePtr = name;
N
printf("\n%c\n",*(namePtr+7));// Output: o
SO
}
EL
Output:
N
LY
SA
ZA
BI
U
AS
IR
by
As you know, an array is a collection of a fixed number of values. Once the size
of an array is declared, you cannot change it.
Sometimes the size of the array you declared may be insufficient. To solve this
issue, you can allocate memory manually during run-time. This is known as
dynamic memory allocation in C programming.
C malloc()
Syntax of malloc()
Example
N
The above statement allocates 400 bytes of memory. It's because the size of the
SO
float is 4 bytes. And, the pointer ptr holds the address of the first byte in the
EL
N
allocated memory.
LY
SA
The expression results in a NULL pointer if the memory cannot be allocated.
ZA
BI
C calloc()
U
AS
The malloc() function allocates memory and leaves the memory uninitialized,
ed
whereas the calloc() function allocates memory and initializes all bits to zero.
ar
ep
pr
Syntax of calloc()
Example:
Dynamically allocated memory created with either calloc() or malloc() doesn't get
freed on their own. You must explicitly use free() to release the space.
Syntax of free()
free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
N
SO
EL
Example 1: malloc() and free()
N
LY
// Program to calculate the sum of n numbers entered by the user
SA
ZA
BI
#include <stdio.h>
U
AS
#include <stdlib.h>
IR
by
int main() {
ed
ar
N
SO
EL
N
LY
SA
Here, we have dynamically allocated the memory for n number of int.
ZA
BI
U
AS
#include <stdio.h>
pr
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
N
return 0;
SO
EL
}
N
Sample Output
LY
SA
ZA
BI
U
AS
IR
by
C realloc()
ed
ar
can change the size of previously allocated memory using the realloc() function.
pr
Syntax of realloc()
Example 3: realloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
N
SO
EL
printf("\nEnter the new size: ");
N
scanf("%d", &n2);
LY
SA
// rellocating the memory
ZA
BI
free(ptr);
return 0;
}
Sample Output
The corresponding memory addresses in decimal are
N
SO
1504083730344972
EL
N
1504083730345036
LY
SA
(Previously allocated )
ZA
BI
and
U
AS
1504083730344972
IR
by
1504083730345036
ed
ar
ep
1504083730345100
pr
(Newly allocated)
N
SO
EL
N
LY
SA
ZA
BI
U
AS
IR
by
ed
ar
ep
pr