In: Computer Science
Write a program (in Q0.c) to do the following:
In main(), declare an integer x. Print the address of x (using the address-of operator). Pass x as an argument to a function void fooA(int* iptr). (Hint: can you pass x itself directly?)
In fooA(int* iptr), print the value of the integer pointed to by iptr, the address pointed to by iptr, and the address of iptr itself.
In the main function, following the call to fooA(...) , print the value of x.
Implement function int fooB(int* a, int *b, int c) which should perform the following computations:
Assign a + b to c.
Set the value pointed to by a to the value pointed to by b.
Set the value pointed to by b to double its original value.
Return the value of c.
#include <stdio.h>
void fooA(int* iptr);
int fooB(int* a, int *b, int c);
int main(void)
{
int x,y;
int *a,*b,c;
*a =
5;
//initialize values pointed to by pointers a and b
*b = 3;
printf("Address of x :%u",&x);
fooA(&x);
//call to function fooA()
printf("\nx = %d",x);
y=fooB(a,b,c);
//call to function fooB()
printf("\nfooB returns : %d",y);
return 0;
}
void fooA(int* iptr)
{
printf("\nvalue of the integer pointed to by iptr
%d",*iptr);
printf("\naddress pointed to by iptr %u",iptr);
printf("\naddress of iptr: %u",&iptr);
}
int fooB(int* a, int *b, int c)
{
c = *a +*b;
*b = *a;
*b = 2 * (*b);
return c;
}
output:
Address of x :4287071804 value of the integer pointed to by iptr 1433419341 address pointed to by iptr 4287071804 address of iptr: 4287071776 x = 1433419341 fooB returns : 8