Pointers are used everywhere in C, and if you have a good understanding of them C should not pose a problem. Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program’s efficiency, and even allow you to handle unlimited amounts of data.

C uses pointers in three main ways. First, they are used to create dynamic data structures: data structures built up from blocks of memory allocated from the heap at run-time. This is the only visible way that Pascal uses pointers. Second, C uses pointers to handle variable parameters passed to functions. And third, pointers in C provide an alternative means of accessing information stored in arrays, which is especially valuable when you work with strings. There is an intimate link between
arrays and pointers in C.

Pointer is a derived data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like structure, union, enum or derived data type like array, function, pointer etc. An integer pointer points to integer data, functional pointer points to a particular function depends
upon its prototype and so on…

All the operations performed on the pointers done through two unary operators

1) Address operator (&) returns the address of a particular variable.
2) Pointer operator (*) returns the data stored in a particular address.
To declare a pointer you have to put an * in front of its name.
Syntax:
data_type *pointer_name;
e.g.
long *ptr;
int (*ptr)();
int (*ptr)[2];
In c programming every variable keeps two type of value.

  • Contain of variable or value of variable.
  • Address of variable where it has stored in the memory.
    Simple pointer declaration and definition:
    int a=5;
    int * ptr;
    ptr=&a;
    Explanation:
    About variable a:
  • Name of variable : a
  • Value of variable which it keeps: 5
  • Address where it has stored in memory : 1025 (assume)
    About variable ptr:
  • 4. Name of variable : ptr
  • 5. Value of variable which it keeps: 1025
  • 6. Address where it has stored in memory : 5000 (assume)

Pictorial representation:

Pictorial representation of pointer

Pointer is of two types:

1) Typed pointer: Typed pointer always points a particular data such as int, char, float…….
2) Untyped pointer: Untyped pointer is nothing but void pointer which can points to any king of
data but initially points to unknown.
#include<stdio.h>
int main()
{
int *ptr_A; /* A typed pointer */
void *ptr_B; /* A untyped pointer */
return 0;

The following example describes that in how many ways we can access a variable using pointers without disturbing the corresponding variable.
#include<stdio.h>
int main()
{
int i=100,*ptr;
ptr=&i;
printf(“%u\n%u\n%u\n%u\n%u\n%u”, i , *ptr, *(&ptr), ptr, &i, &ptr);
}

What is the size of Pointer variable in C?

All data is stored in memory. But different data types occupy different amount of memory. The sizeof() operator in C can be used to determine the number of bytes occupied by each data type. For example, on some machine you may have
sizeof(int) = 2
sizeof(float) = 4
sizeof(double) = 8
These numbers are Not the same for all machines. You should use the sizeof() operator instead of assuming the value.

  • Size of Pointer can be evaluated by using sizeof operator
  • Pointer stores the address of the Variable.
  • Address of variable is nothing but the integer value.
  • In C, for storing integer value 2 bytes are required.
  • So Pointer variable requires 2 bytes of memory.

#include <stdio.h>
int main()
{
int a[10];
int *a1;
printf(“%d\n”, sizeof(a));
printf(“%d\n”, sizeof(a1));
getchar();
return 0;
}
/* my output
20
2
*/

CALL BY VALUE & CALL BY REFERENCE:

1) Call by Value

  • While calling a Function, if we pass values as arguments to the called function known as call by value.
  • So that the Arguments those are passed to that function just contains the values from the variables but not an Actual Address of the variable.
  • So that generally when we call a Function then we will just pass the variables or the Arguments and we doesn’t Pass the Address of Variables ,
  • So that whatever happened in the called function which never effects on the Values or on the variables. In the calling function

#include <stdio.h>
void main()
{
int x, y, temp;
clrscr();
printf(“Enter the value of x and y\n”);
scanf(“%d%d”, &x, &y);
printf(“Before Swapping\nx = %d\ny = %d\n”,x,y);
swap(x,y);
getch();
}
Void swap(int a,int b)
{
Int temp;
temp = a;
a = b;
b = temp;
printf(“After Swapping\nx = %d\ny = %d\n”,a,b);
}

2) Use of pointers in Call By Reference :-

  • While calling a function if we pass address of actual parameters as arguments to the formal parameters in the calling function known as call by reference.
  • In call by reference mechanism, if any changes happened on the formal parameters in the called function directly affects on the actual parameters in the calling function.

#include<stdio.h>
#include<conio.h>
void swaping(int *x, int *y);
int main()
{
int n1,n2;
printf(“Enter first number (n1) : “);
scanf(“%d”,&n1);
printf(“Enter second number (n2) : “);
scanf(“%d”,&n2);
printf(“\nBefore swapping values:”);
printf(“\n\tn1=%d \n\tn2=%d”,n1,n2);
swaping(&n1,&n2);
printf(“\nAfter swapping values:”);
printf(“\n\tn1=%d \n\tn2=%d”,n1,n2);
getch();
return 0;
}
void swaping(int *x, int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf(“\nAfter swapping values:”);
printf(“\n\tn1=%d \n\tn2=%d”,*x,*y);
}

Scroll to top