logo codesdope

Pointers in C


We will discuss pointers here. Playing with pointers in C is really fun but pay a little more attention in this chapter because pointers are considered to be the toughest part of the C.

Now, let's again have a look at the declaration

int a= 44;

As we have already seen, it is like

44
a

Now let's go to the undiscussed part. As we all know that when we declare 'a', it is given a memory location and the value of 'a' is stored in that memory location. In the world of programming, 'a' will also have an address. So, this address is the address of that memory location in which the value of 'a' is stored.

Address of 'a' is an integer which is something like 9562628. It will vary for every computer as per memory given to 'a' at that time.

pointers in C

Now coming to pointer, a pointer points to some variable, that is, it stores the address of a variable. E.g.- if 'a' has an address 9562628, then the pointer to 'a' will store a value 9562628 in it. So, if 'b' is a pointer to 'a' and the value of 'a' is 10 and its address is 9562628, then 'b' will have a value 9562628 and its address will be something different.

pointers in C

Address in C is represented as &a, read as address of a. Remember that all the time when we were taking value of 'a' using 'scanf', we were taking an input from user and storing it at the address of 'a', i.e. in 'a'.

How to Use Pointers?


You must be enjoying programming in C, and will do even more now. Till now, we have just seen what is a pointer. So, 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).

Or we can say that 'b' will store the address of 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.

So,
*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'

So, in short,
int a; - 'a' is an integer.
int *b; - 'b' is an pointer to an integer.
b = &a; - 'b' is now pointing to 'a'.
'*b' will now represent a.

Example of pointers.

#include <stdio.h>
int main()
{
    int a = 10;
    int *p;
    p = &a;
    printf("p = %u\n", p);
    printf("*p = %d\n", *p);
    printf("&p = %u\n", &p);
    printf("*&p = %u\n", *&p);
    return 0;
}
Output
p = 2383619684
*p = 10
&p = 2383619688
*&p = 2383619684
Value of address will vary every time you run because everytime a new memory will be allocated.
'%u' is used for memory address to give unsigned (positive) values.

As discussed earlier, 'p' is a pointer to 'a'. 'a' has the value of 10. So, '*p' will be 10. 'p' will store address of a. Thus, p = 2383619684 would imply that 2383619684 is the address of 'a'. Now, '&p' represents address of 'p'. So, it is 2383619688. Notice that it is 4 more than the address of 'a' since int is of 4 bit and so is 'a'. Now, '*&p' will be the value of '&p' and the value of '&p' is the address of 'a'. So, it is 2383619684.

Passing Pointers to Function


Let's first consider an example that will swap two numbers i.e. interchange the values of two numbers.

#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;
}
Output
Enter first number
2
Enter second number
4
First number : 4
Second number : 2

Swapping means to interchange the values.
void swap( int *a, int *b ) - It means our function 'swap' is taking two pointers as argument. So, while calling this function, we will have to pass the address of two integers ( call by reference ).
int t; t = *a; - We took any integer t and gave it a value '*a'.
*a = *b - Now, *a is *b. This means that now the values of *a and *b are equal to that of *b.
Since 't' has a initial value of '*a', therefore, '*b' will also contain that initial value of '*a'. This means that we have interchanged the values of two values.

Since, we have done this swapping with pointers ( we have targeted on address ), so, this interchanged value will also reflect outside the function and the values of 'num1' and 'num2' also get interchanged.

In the above example, we passed the address of the two variables (num1 and num2) to the swap function. The address of num1 is stored in 'a' pointer and that of num2 in 'b' pointer. In the swap function, we declared a third variable 't' and the value of 'a' and 'b' (and thus that of num1 and num2 )gets swapped.

In normal function call ( call by value ), the parameters of a function are xerox copies of the arguments passed to the function. It is like we are passing xerox copies.

So altering them won't affect the real values

But in call by referance, we pass the address of variables to the function. Passing address is like passing original 'x' and 'y'.

Altering the parameters will alter the real values also.

In the swapping example also, we used call by reference in which we passed the address of num1 and num2 as arguments to the function. The function parameters 'a' and 'b' points to the address of num1 and num2 respectively. So any change in the parameters 'a' and 'b' changes the value of num1 and num2 also.

Let's see a normal function call (passing values and not address) and try to alter the value of the variable.

#include <stdio.h>
void swap( int a, int b ) /* function */
{
    int c;
    c = a;
    a = b;
    b = c;
    printf("After swapping: In function\n");
    printf("First number: %d\n", a);
    printf("Second number: %d\n", b);
}
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("After swapping: In main program\n");
    printf("First number: %d\n", num1);
    printf("Second number: %d\n", num2);
    return 0;
}
Output
Enter first number
2
Enter second number
4
After swapping: In function
First number: 4
Second number: 2
After swapping: In main program
First number: 2
Second number: 4

In the above example, we are trying to interchange the values of two variables. We passed num1 and num2 as arguments in the function swap. 'a' and 'b' are the copies of 'num1' and 'num2' respectively. In the swap function, the values of 'a' and 'b' got interchanged while the values of 'num1' and 'num2' remained unchanged.

So, you saw that the numbers got swapped inside the function but outside it, there was no change. Because by passing values to functions, copies of the values got passed and not the real values. So, outside the function there was no effect on the variables.

If you have any confusion left, then just go through the above two codes again and you will understand it. If not, then you can always raise your doubts in the discussion section.

Pointers 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.

Don't worry about failure. You only have to be right once.
-Drew Houston

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.