Pointers
Pointers
• Since x is an integer variable, it will be allocated 2 bytes or 4 bytes based on the system
used.
• Assuming that the compiler assigns it memory locations 1003 and 1004, the address of x
(written as &x) is equal to 1003, that is the starting address of x in the memory. When we
write, ptr = &x, then ptr = 1003.
• We can ‘dereference’ a pointer, i.e., we can refer to the value of the variable to which it
points by using the unary * operator as in *ptr. That is, *ptr = 10, since 10 is the value of
x.
#include <stdio.h>
int main()
{
int num, *pnum;
pnum = #
printf("\n Enter the number : ");
scanf("%d", &num);
printf("\n The number that was entered is : %d", *pnum);
return 0;
}
Output
Enter the number : 10
The number that was entered is : 10
• In C, since you cannot have a variable of type void, the void pointer will therefore not point to any data and,
thus, cannot be dereferenced.
• You need to cast a void pointer to another kind of pointer before using it.
• Generic pointers are often used when you want a pointer to point to data of different types at different times.
• It is always recommended to avoid using void pointers unless absolutely necessary, as they effectively allow
you to avoid type checking.
Generic Pointers
#include <stdio.h>
int main()
{
int x=10;
char ch = ‘A’;
void *gp;
gp = &x;
printf("\n Generic pointer points to the integer value = %d", *(int*)gp);
gp = &ch;
printf("\n Generic pointer now points to the character= %c", *(char*)gp);
return 0;
}
• Output
• Generic pointer points to the integer value = 10
• Generic pointer now points to the character = A
Passing arguments to functions using pointers
• Pointers provide a mechanism to modify data declared in one
function using code written in another function.
• If data is written in fuc1() and we want to write code in func2() that
modifies the data in func1(), then we must pass the addresses of the
variables to func2().
• The calling function sends the addresses of the variables and the
called function declares those incoming arguments as pointers.
• Inorder to modify the variables sent by the calling function, the called
function must dereference the pointers that were passed to it.
• Thus, passing pointers to a function avoids overhead of copying data
from one function to another function.
Passing arguments to functions using pointers
• To use pointers for passing arguments to a function, the programmer
must do the following:
• Declare the function parameters as pointers.
• Use the dereferenced pointers in the function body.
• Pass the addresses as the actual argument when the function is called.
Write a program to add two integers using pointers and functions
#include <stdio.h>
void sum (int*, int*, int*);
int main()
{
Output
Enter the first number : 23
int num1, num2, total; Enter the second number : 34
printf("\n Enter the first number : "); Total = 57
scanf("%d", &num1);
printf("\n Enter the second number : ");
scanf("%d", &num2);
sum(&num1, &num2, &total);
printf("\n Total = %d", total);
return 0;
}
void sum (int *a, int *b, int *t)
{
*t = *a + *b;
}
Drawbacks of pointers
• If used incorrectly, pointers can lead to bugs that are difficult to resolve.
• For example, if you use a pointer to read a memory location but that pointer is pointing to an incorrect
location, then you may end up reading a wrong value. An erroneous input always leads to an erroneous
output. Thus however efficient your program code may be, the output will always be disastrous. Same is the
case when writing a value to a particular memory location.