void funct(int i = 10, int j = 20)
{
  int *ptr = &i;
  ptr++;
  printf("%d", *ptr);
}

int main(int argc, char* argv[])
{
  funct();
  funct(20, 30);
}

Ans: 20 and 30

This is a C++ specific question. We have taken a local pointer of integer and assigned the address of first argument i.e. the address of i. Arguments are pushed in a C function sequentially from right to left. Stack pointer decrements by sizeof(int) with every argument push. Thus the first argument points at the lowest address and the last argument points at the highest address.

Address Stack frame Value
0xA0008 j 30
0xA0004 i 20
0xA0000 return address of funct() code address of next to call

Now when we increment the pointer it will point to the next argument i.e. the address of j. So, value of the pointer is the value of j itself. For the first function call, the value is the default value of j i.e. 20. But for the next function call, we have given the value 30 explicitly. Thus the value will be 30.

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#