Question 5 || Hacker Rank
Pointers in C || Hacker Rank
Objective
In this test, you will figure out how to execute the essential functionalities of pointers in C. A pointer in C is a method for sharing a memory address among various settings (basically works). They are principally utilized at whatever point a capability needs to change the substance of a variable that it doesn't possess.
To get to the memory address of a variable, , prepend it with sign. For instance, &val returns the memory address of .
This memory address is relegated to a pointer and can be divided between different capabilities. For instance, will allot the memory address of to pointer . To get to the substance of the memory to which the pointer focuses, prepend it with a *. For instance, *p will return the worth reflected by and any alteration to it will be reflected at the source ().
void increment(int *v) {
(*v)++;
}
int primary() {
int a;
scanf("%d", &a);
increment(&a);
printf("%d", a);
bring 0 back;
}
Task
Complete the capability void update(int *a,int *b). It gets two whole number pointers, int* an and int* b. Set the worth of to their aggregate, and to their outright contrast. There is no return esteem, and no return articulation is required.
Input Organization
The info will contain two whole numbers, and , isolated by a newline.
Yield Configuration
Adjust the two qualities set up and the code stub principal() will print their qualities.
Note: Info/ouput will be consequently dealt with. You just need to finish the capability portrayed in the 'task' segment.
Test Info
4
5
Test Result
9
1
#include <stdio.h>
void update(int *a,int *b);
int main()
{
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
void update(int *a,int *b)
{
int sum = *a + *b;
int diff = abs(*a - *b);
*a = sum;
*b = diff;
}
0 Comments