Functions
Array

Pointers

Pointer in C

Pointer in a c language is essentially a simple integer variable which holds a memory address that points to a value, instead of holding the actual value itself.

The computer’s memory is a sequential store of data, and a pointer points to a specific part of the memory. Our program can use pointers in such a way that the pointers point to a large amount of memory – depending on how much we decide to read from that point on.

pointers in c-language-codingknack

Use of Pointer

let’s introduce pointer in our program.

int a = 44;
int *b; /* declaration of pointer b */
b = &a;

int *b This statement should mean that ‘*b’ is an integer but then what is significance of ‘*’ before ‘b’? It means that b points to some integer (‘b’ is a pointer to some integer).

b = &a; – As said, ‘b’ will store the address of some integer because it is a pointer to an integer. In this declaration, it is storing the address of ‘a’. Since, ‘b’ is a pointer and ‘&a;’ represents address, so, by declaring ‘b = &a;’ means that b will now store address of a.

*b is the value of the variable ‘b’ is pointing to. Here *b is 44. As stated earlier, int *b;’ means that ‘*b’ is an integer, but ‘*’ before means that b is a pointer. So, ‘*b’ will be the value of the variable to which ‘b’ is pointing. Here, ‘b’ is pointing to ‘a’, therefore ‘b’ will store the address of ‘a’ and ‘*b’ will be the integer to which ‘b’ is pointing i.e. ‘a’

Lets take an example of pointer in c.

#include<stdio.h>
int main()
{
   int *ptr, q;
   q = 50;
   /* address of q is assigned to ptr */
   ptr = &q;
   /* display q's value using ptr variable */
   printf("%d", *ptr);
   return 0;
}

 

Passing Pointer to Funtion

Try this same program without pointer, you would find that the bonus amount will not reflect in the salary, this is because the change made by the function would be done to the local variables of the function. When we use pointers, the value is changed at the address of variable

#include <stdio.h>
void salaryhike(int  *var, int b)
{
    *var = *var+b;
}
int main()
{
    int salary=0, bonus=0;
    printf("Enter the employee current salary:"); 
    scanf("%d", &salary);
    printf("Enter bonus:");
    scanf("%d", &bonus);
    salaryhike(&salary, bonus);
    printf("Final salary: %d", salary);
    return 0;
}

 

Enter the employee current salary:10000
Enter bonus:2000
Final salary: 12000

Lets take anothr example.

#include<stdio.h>
void swap( int *a, int *b )
{
    int t; 
    t = *a; 
    *a = *b; 
    *b = t; 
}
int main() 
{
    int num1, num2; 
    printf("Enter first number\n");
    scanf("%d", &num1);
    printf("Enter second number\n");
    scanf("%d", &num2);
    swap( &num1, &num2);
    printf("First number : %d\n", num1);
    printf("Second number : %d\n", num2);
    return 0;
}

Enter first number
2
Enter second number
4
First number : 4
Second number : 2

Pointer to Array

We also have pointers to array which we will see in the next topic Array.Before moving to the next topic, practice a lot of problems on pointers so that you have a strong grip over it.

Comments are closed.