Find the address of variables in the main?

While browsing some C ++ blogs recently, I came across a small C-teaser program in one of them.

#include<stdio.h>

int find_addr()
{
/*fill your code here*/
}

int main()
{
int i,j;
clrscr();
find_addr();
return 0;
}

The question is to find the address of the variables i and j without touching the main function. I still could not figure it out. It feels terrible that I could not even solve this secondary question: ((.

EDIT:

There were many non-standard statements in the above program, such as using conio.h anbd of other non-standard headers and its functions, getch () and other operators, I edited it in a hurry and forgot to omit void from void main (), sorry for that.

EDIT2: I gave my only vote to close this topic as I perceive the answers posted here that there are non-standard questions related to the question.

+3
4

, . noeless main(), , , void main(). / - . . , .

, :

? , ! .

main()
{
    int i = 300;
    char *ptr = &i;
    *++ptr = 2;
    printf("%d",i);
    getch();
}

:

, 65486?

void main()
{
    int num[] = {10,11,12,13};
    printf("%u %u",num,&num);
}

, . , !

+6

, , , .

int find_addr()
{
  int t;
  int* i_addr = &t - <some platform&compiler&whatever specific constant>;
  int* j_addr = &t - <some platform&compiler&whatever specific constant>;
}

, i j , , .

, i j, - . , .

+5

_AddressOfReturnAddress intrinsic. , . i,j .

, "" , , . - - j. , j, , . i, j , - , , , .

+1

, VC..

2 int32 , a.

int find_addr()
{

int a;
printf("&a = %u &i = %u & j = %u\n", &a, &a+4, &a+3);

}
0

Source: https://habr.com/ru/post/1729050/


All Articles