pointer in c hackerrank solution
C Programming c programming notes for gate C programming Tutorials gate study material for cse

Pointer in C Hackerrank Solution

Pointer in C Hancerrank Solution

In this Pointer in C Programming Tutorial at first we will learn about basic concepts of pointer after that we will see some pointer in c hackerrank solution for the practice of computer science students.

Frequently asked Questions

Some frequently asked questions from pointer in c programming are given below

  • What is Pointer in C ?
  • What is Call by Reference in C ?
  • What is pointer to pointer in c ?
  • Pointer in C hackerrank solution

 What is Pointer in C ?

In C Programming a Pointer is variable which store the address of another variable.

The general syntax for declare a pointer is as follows:-
                               data type * P
For example: –    if we define.
                                   int * p ;
The meaning of this line is that p is a pointer of integer type i.e. it store the address of integer type variable it means p is an address at which an integer type value is stored.
If we declare char * p it means P is a pointer to character. P is an address at which a character is stored.
Similarly float * p means p is an address at which float type value is stored.
Here important point is that since pointe is an address so it always will be an integer value.

Initialization of Pointer

Pointer variable is initialized using & ( address operator ).
We can understand initialization of pointer with the help of an example.
Consider a small program given below
#include<stdio.h>
#include<conio.h>
void main()
{
   int a=10;
int *p;
p=&a;    // assign address of variable a to pointer p
printf(“%d”,a);
printf(“%d”, p);
getch();
}
Output
10
65522
Here 65522 is the value of P it means address of a.

Call By Reference in Pointer

Call by reference is a way of parameter passing to function using pointer. If we have to change the value of actual parameters by changing the formal parameters then we use call by reference method.
Consider the following program to swap the value of two variables.

#include<stdio.h>
void swap(int *x, int *y);
int main()
{
int m=22;
int n=44;
printf(“before swap m=%d and n=%d\n”,m,n);
swap(&m,&n);
printf(“after swap m=%d and n=%d\n”,m,n);
return 0;
}
void swap(int *x,int *y)
{

printf(“value of x=%u value of y =%u\n”,x,y);
printf(“before swap value stored at address x=%d and value stored at address y =%d\n”,*x,*y);
int temp;
temp=*x;
*x=*y;
*y=temp;
//s
printf(“value of x=%u value of y =%u\n”,x,y);
printf(“After swap value stored at address x=%d and value stored at address y =%d\n”,*x,*y);
}

Output

call by reference

In the above program x and y are the memory address of m and n because in function calling address of m and n is passed.

Since x and y are the address so address remain same after swap but value stored at address x  and address y will be swap.

Pointer to Pointer

We can also declare a pointer to pointer in C Programming.  We can understand this using following program for pointer to pointer.

#include<stdio.h>
#include<conio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf(“address of a: %u\n”,p);
printf(“address of p: %u\n”,pp);
printf(“value stored at p: %d\n”,*p);
printf(“Value represented by double pointer %d\n”,**pp);
getch( );
}

output

pointer to pointer in c

In the above Program pointer p is pointing to the address of a and pointer pp is a double pointer pointing to the address of pointer p.

In the next section we will discussed pointer in c hackerrank solution.

Pointer in C Hackerrank Solution

In this section we will understand the solution of problem on hackerrank based on pointer in c.

Problem  

Complete the function void update(int *a,int *b). It receives two integer pointers, int* a and int* b. Set the value of a to their sum, b and to their absolute difference. There is no return value, and no return statement is needed.

a’ = a+b

b’ = |a-b|

Input Format

The input will contain two integers, a and b, separated by a newline.

Output Format

Modify the two values in place and the code stub main() will print their values.

Note: Input/ouput will be automatically handled. You only have to complete the function described in the ‘task’ section.

Sample Input

4
5

Sample Output

9
1

Explanation

a’ = 4 + 5 = 9

b’ = | 4 – 5 | = 1

Solution 

Program to solve the above problem is given below

#include <stdio.h>
#include<conio.h>
void update(int *a,int *b) {
// Complete this function
int sum,diff;
sum = *a+*b;
diff = abs(*a-*b);
*a = sum;
*b = diff;
}

int main() {
int a, b;

int *pa = &a;
int *pb = &b;
clrscr();
scanf(“%d %d”, &a, &b);
update(pa, pb);
printf(“after update\n %d\n%d”, a, b);
getch();
return 0;

}

Output

pointer in c hackerrank solution

 

For the above pointer in c hackerrank solution we use the following steps in the above program.

Step 1 – Included the required header files.

Step 2-  Defined a function update( ) that takes two integer pointers, a and b, as arguments. We also declared two integer variables, ‘sum’ and ‘diff’. sum will store the sum of two integers, and diff will store the absolute difference.

Step 3 – Declared the main function. Inside main( ) function, we declared two integer variables, a and b, and two integer pointers, pa and pb, that point to a and b.

Step 4- We have used the scanf( ) function to read the inputs and called the update function to update the values of a and b.

Step 5– Display the result using printf( ) function.

Conclusion and Summary

In this C Tutorial of Pointer we have explained the basic concept of pointer. we have also explained the pointer in c hackerrank solution.

I hope this tutorial will be beneficial to students.

 

Leave a Reply

Your email address will not be published. Required fields are marked *