Pointer in C

Hey guys.

We are here again and today we’ll talk about ‘Pointers”.

coding

But before that, if you didn’t read our previous posts till now then go and read them first from here- C language.

We are going to a see a very important concept and also one of the most difficult concepts of C language.

So, coders, the below elucidation will make the whole concept of pointers in C crystal clear.

The reason why the pointers become a tough nut to crack has much to do with the pointer terminology than the actual concept. To start with the definition, a pointer in C is a variable that stores the address of another variable.

But we can have a variable of many types, it could be an int, float, char, array, or function. But the point to be well aware of is that pointer has always an int datatype since it stores the address and the address is always an integer number. So, next comes the question of size of pointers.

Before going further, again I would like to inform you that the pointer is just a variable storing address of another variable.

Size of Pointers

learning

So, since the data type of the pointer is int therefore the size of the pointer depends upon the architecture. In a 32-bit architecture, the size of int is 2 bytes.

Now let us consider a basic example that helps to get the basic idea of pointers more clearly.

Here, i is the name given to the memory block that stores a value 72 which is of int type and the address of this memory block is for say, 87994 and j is the name of the memory block that stores the address that is 87994. Now, since j is just the name of a memory block j also has an address.

A program to demonstrate pointers.


#include <stdio.h>
int main()
{
int i = 8;
int *j;
j = &i;
printf("address of i = % un",&i);
printf("address of i = % un", j);
printf("address of j = % un", &j);
printf("value of i = % dn", i);
printf("value of i = % dn", *(&i));
printf("value of i = % dn", *j);
return 0;
}

Now, the ‘&’ used in the first printf() is the ‘address of’ operator.

The ‘%u’ used in the first three printf() is the format specifier for printing address(an unsigned integer).

Now, the next new thing that comes is ‘*’(indirection operator), this is called the ‘value at address’ operator and is used to obtain the value present at a given memory address. Also, in the fifth line of code j is declared as ‘int *’ it is used to tell the compiler that j is a special type of variable storing a value of another variable of int type.

If we have char or float pointer then we would be using char * or float* respectively.

For example if my variable is a char

#include <stdio.h> int main() { char i = 'A'; char *j; j = &i; printf("address of i = % un", &i); printf("address of i = % un", j); printf("address of j = % un", &j); printf("value of i = % cn", i); printf("value of i = % cn", *(&i)); printf("value of i = % cn", *j); return 0; }

For example, if my variable is a float

#include <stdio.h> int main() { float i = 5.89; float *j; j = &i; printf("address of i = % un", &i); printf("address of i = % un", j); printf("address of j = % un", &j); printf("value of i = % fn", i); printf("value of i = % fn", *(&i)); printf("value of i = % fn", *j); return 0; }

Now, let’s see the further extended concept of pointers

Pointer to Pointer

Just like j is a pointer to i or storing the address of i, we can have another variable that can further store the address of j. What will be the type of k then?

If I say pointers obviously, I make sense by now, but if I say pointers to pointers, it seems a little weird! but it obviously it makes sense in C.

#include <stdio.h> int main(){ int i=3,*j,**k;//here j and k are special type of variables that is called pointer variable j=&i;//j is given value address of i k=&j;//k is given value address of j printf("Address of i= %un",&i);//ampersand i(&i) is telling the compiler to print the address of variable i printf("Address of i= %un",j);//since j was a special type of variable the format specifier used here is %u instead of %d. printf("Address of i= %un",*k);//*k means that we are telling compiler to print the value at address of k printf("Address of j= %un",&j); printf("Address of j= %un",k); printf("Address of k= %un",&k); printf("Value of j= %un",j); printf("Value of k= %un",k); printf("Value of i= %un",i); printf("Value of i= %un",*(&i)); printf("Value of i= %un",*j); printf("Value of i= %un",**k); return 0; }

Looking at the above figure we can clearly get that how the hierarchy goes i is equal to 3 and j stores the address of i so, j is the pointer of i while k, in turn, stores the address of j and is pointer to pointer. Well, this does not stop. This series can go on, just for example we would have a pointer to a pointer to a pointer, but talking about the use we generally don’t get to use those. The point to remember is that while declaring k we wrote the line as int **k; moving forward with the hierarchy we will have to increase the asterisks.

Another thing, when you run this program, the addresses that get printed might be something different from that shown here. The address varies from compiler to compiler. However, for any address, the relationship between i, j, and k would remain the same.

#Important fact

If the address is known we can change the value of a variable, but if the value of the variable is known we can’t change the address.

Types of Function Call

As in the chain of series of Learning C language we had come across Functions and types of a function call in our earlier articles, we have two types of function call-

1)Call by Value

2)Call by Reference

Let’s start with examples.

Call by Value

#include <stdio.h> void swap(int x, int y); int main(){ int a=3,b=6; swap(a,b); printf(“a=%d b=%dn”,a,b); return 0; } void swap(int a, int b) { int t; t=a; a=b; b=t; printf("Values of a and b is %d %dn",a,b); }

Meaning of Call by Value-

Whenever a function is called by value, it means that the called function creates a copy of the passed object. This can be proved by checking on this example.

#include <stdio.h> int sum(int a, int b); int main(){ int s=sum(3,4); printf("%d",s); } int sum(int a, int b){ return a+b; a=5; }

As normally we would be thinking that the output of this code would be 9 since a=5 and b=4 but since the copy of the object is created in the call by value without evoking the constructor is passed through the function the value of a does get changed and we finally obtain output as 3+4=7.

Call by Reference

#include <stdio.h> void swap(int *, int *); int main() { int a=3,b=6; swap(&a,&b); printf(“a=%d b=%dn”,a,b); return 0; } void swap(int *x, int *y) { int t; t=*x; *x=y; *y=t; printf(“x=%d y=%dn”,x,y); }

Meaning of Call by Reference-

If we want to the called function to work with the original object then we can use the reference or pointers as to the argument to the function. When we pass the pointers to pointers what happens is that the addresses of the actual arguments are copied into the corresponding formal arguments of the function.

#include <stdio.h> #include <stdlib.h> int main(){ int a, b, Sum; printf("Enter the two number: "); scanf("%d %d", &a, &b); Sum= sum(&a, &b); printf("sum of %d and %d is %d", a, b,Sum); } int sum(int *no1, int *no2) { int ans; ans = *no1 + *no2; return ans; }

Now, let’s see an important question,

Write a program using a function that calculates the sum and average of two numbers using pointers and prints the values of sum and average in main ().

#include <stdio.h> void SumandAvg(int a,int b,int *sum, float* avg){ //SumandAvg function has two exact values and two addresses as its argument. *sum=a+b; *avg=*sum/2; } int main(){ int i,j,sum; float avg; i=3; j=9; SumandAvg(i,j,&sum,&avg); printf("The Sum of two numbers is %dn",sum); printf("The Average of two numbers is %fn",avg); }

Output:

The sum of the two numbers is 12

The average of the two numbers is 6.000000.

Here, in the above problem we have overcome the limitation of the return statement that is it can be used only to return one value from a function at a time, by using call by reference here we made our function ‘SumandAvg’ to return two values at the same time.

So, that’s all. Thank you all. If you have any question then feel free to ask in the comment box.

Harshita Raj
Harshita Raj
Articles: 9

Leave a Reply

Your email address will not be published.

You cannot copy content of this page