I am new to stackoverflow, so I apologize in advance for any errors that I make.
I ran into this C puzzle recently. The program is given below.
#include<stdio.h> void change() { } int main() { printf("\nHello"); change(); printf("\nHai"); printf("\nHow are you?"); return 0; }
Expected Result:
Hello Hai How are you?
The task requires changing the output as follows by adding some code to the change () function
Hello How are you?
You should not make any changes to main ().
I tried to change the return address of the change () function stored in the stack memory, and there, avoiding the printf ("\ nHai") operator. But I get errors when compiling using gcc.
The code I added to change () is shown below.
void change() { char ch; *(&ch+10)+=20; }
Values ββadded to ch (10 and 20) are fixed with
objdump -d ./a.out
I hope to get some suggestions to solve the problem. Thank you for your time and patience.
source share