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.

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

 Vote 0

Similar topics related to this section

structure as function argument, array as function argument, inline-functions, macro vs inline-function, function pointer, default C++ argument value, Recursion, factorial using recursion, Fibonaci using recursion,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan