0

This was actually asked in my college quiz in C language.

a. Value is not printed

b. 100

c. Address of the variable n;

d. Garbage value

I marked option 'c' as createarray returns the address of n

But on the answer key and many people are arguing that it will option d.

So can you help in understanding this.

#include <stdio.h>
int *createarray();
int main() 
{
    int *a;
    a=createarray();
    printf("%d",a);
    return 0; 
}
int * createarray() 
{
    int *p,n=100; 
    p=&n;
    return p; 
}
  • 5
    The value is "garbage" in the sense that the pointer does not point to a valid object because `n` no longer exists after the function exits. – kaylum Apr 11 '20 at 06:46
  • 6
    `a` is pointer. To show the value in `a`, the correct code is `printf("%p\n", (void *)a);` Using `%d` for a pointer is not valid, and the result will be garbage on many systems. – user3386109 Apr 11 '20 at 06:48
  • 3
    Program has undefined behavior, so there is no correct answer. All of the proposed ones could happen. Or something else. – Mat Apr 11 '20 at 06:50
  • @Mat Can you please Help me understand it more. The program has undefined behavior because the address it's pointing to is no longer exist? Or any other reason – Deepanshu Singh Apr 11 '20 at 06:54
  • 1
    @DeepanshuSingh: the format string doesn't match the argument type https://stackoverflow.com/questions/14504148/what-can-happen-if-printf-is-called-with-a-wrong-format-string – Mat Apr 11 '20 at 06:58
  • 1
    And if it did match the format string, the answer would still be garbage because there cannot be the address of the variable `n` given that the variable `n` [no longer exists](https://stackoverflow.com/a/6445794/11683). – GSerg Apr 11 '20 at 07:02
  • @4386427 it is valid to print that value, and it might even be a [valid pointer value in itself](https://stackoverflow.com/a/24398510/11683), but option `c` calls for *address of variable `n`*, not for *a valid pointer value*. There is no variable `n` anymore. – GSerg Apr 11 '20 at 07:20
  • @GSerg oh - I see - I misunderstood your comment. So I have deleted my comment. – Support Ukraine Apr 11 '20 at 07:22

1 Answers1

3

The printf format string is wrong: %d is used to print integers and you are giving it a pointer, so the program has undefined behavior. So as written, none of the 4 options are correct (although all are possible except c which is meaningless). If the printf format is fixed to use %p, then the question is what is a, can its value be accessed, and what does the standard say will be the result.

a points to an object that's reached the end of its lifetime, so its value is indeterminate. An indeterminate value may be an unspecified value or a trap representation. In the former case some pointer value will be output, and in the latter case it's undefined behavior as before.

See the C standard (here I'm quoting from the C99 standard):

  • §7.9.6.1.9 (on fprintf, but it also applies to printf): "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined."
  • §6.2.4.2 "The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime."
  • §3.17.2.1 "indeterminate value: either an unspecified value or a trap representation"

So, the least wrong answer to the question is d, but you have to fix the code, and then assume that your compiler produces code that never generates trap representations, and understand "garbage" to mean "unspecified".

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118