0

I, as student, have been assigned to finish this task, pointer in C. I state the problem and question after the codes. The codes are:

#include <stdio.h>
#include <stdlib.h>

int *value(void){
    int i = 3;
    return &i;
}

void callme(void) {
     int x = 35;
}

int main(void){
    int *ip;
    ip = value();
    printf("*ip == %d\n", *ip);
    callme();
    printf("*ip == %d\n", *ip);
}

I tried the codes using Code Blocks software. The problem is, when I build and run (The feature in "Code Block" software to just run through the program without any requirement other than the codes itself) the program, it only shows the execution time, while i need is the value of ip according to the question below. I am still beginner, so bear with me. :-)

The question is "What is the value of ip and explain what happens to ip after each functions have been called?" If able, please answer and explain clearly. If there is anything wrong or not clear, please let me know and comment. Thanks a lot for the help.

  • Why did you post two separate code snippets? Why not post a [mre] as one code snippet? – Andreas Wenzel Jan 30 '22 at 21:12
  • 2
    It's a trick question since the program has undefined behavior, specifically at least the `*ip` dereferences an invalid pointer. Furthermore, even without the UB, the value of `ip` is just some address, nothing more can be said about it. – user17732522 Jan 30 '22 at 21:12
  • 1
    i think the question wants `callme()` to modify where `ip` is pointing since `ip` should be pointing to the first `int` reserved on the stack by a called function, however in practice the code behavior is unidentified and might cause a segmentation fault or a different value than expected based on the machine and OS that executes the code, (and maybe the compiler) – Ahmed AEK Jan 30 '22 at 21:21
  • Umm, @AndreasWenzel, sorry for not uniting the codes above. I am new to this, and thanks for that. I will remember it in the future. – 037 - Muhammad Rafli Fadilah Y Jan 30 '22 at 21:25
  • @037-MuhammadRafliFadilahY: Note that you can [edit] your question to fix it. – Andreas Wenzel Jan 30 '22 at 21:27
  • I see, okay, also the x in the codes isn't related to any codes above or below it? Is it a typo? – 037 - Muhammad Rafli Fadilah Y Jan 30 '22 at 21:28
  • @037-MuhammadRafliFadilahY: The function `callme` effectively does nothing, as the value of the variable `x` has no effect on the remainder of the program. Therefore, the entire function `callme` is probably just a [red herring](https://en.wikipedia.org/wiki/Red_herring). – Andreas Wenzel Jan 30 '22 at 21:35

1 Answers1

4

This function

int *value(void){
    int i = 3;
    return &i;
}

returns a pointer to the local object i with automatic storage duration that will not be alive after exiting the function. So the returned pointer will be invalid.

Dereferencing this pointer in the calls of printf results in undefined behavior.

If you will change the function like

int *value(void){
    static int i = 3;
    return &i;
}

then the program will be correct though the compiler can issue a warning that the declared variable x in the function callme is not used.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335