R
raghu
look at the code below
#include <stdio.h>
int * get_number(int i);
main()
{
int *krrk[5];
int i = 0;
printf(" testing the address of a variable in the function\n i
address in main %u\n", &i);
krrk = get_number(i);
printf("for = i %d address %u and its contents %d\n", i, krrk,
*krrk);
for(i = 1; i < 5; i++)
{
krrk = get_number(i);
printf("for i = %d address %u and its contents %d\n", i, krrk,
*krrk);
printf("for i - 1 = %d address %u and its contents %d\n", i - 1,
krrk[i - 1], *krrk[i - 1]);
}
for( i = 0 ; i < 5; i++)
{
printf(" for i = %d address of %d is %u\n", i, *krrk, krrk);
}
}
int * get_number(int i)
{
return &i;
}
output of this file (I got) is as follows
testing the address of a variable inthe function
i address in main 2280636
for i = 0 address 2280576 and its contents 0
for i = 1 address 2280576 and its contents 1
for i -1 = 0 addrss 2280576 and its contents 4202616
for i = 2 address 2280576 and its contents 2
for i -1 = 1 addrss 2280576 and its contents 4202616
for i = 3 address 2280576 and its contents 3
for i -1 = 2 addrss 2280576 and its contents 4202616
for i = 4 address 2280576 and its contents 4
for i -1 = 3 addrss 2280576 and its contents 4202616
for i = 0 address of 4202660 is 2280576
for i = 1 address of 4202708 is 2280576
for i = 2 address of 4202708 is 2280576
for i = 3 address of 4202708 is 2280576
for i = 4 address of 4202708 is 2280576
my doubt is here I am returning a local variable of a function
and so Its get corrupted for the next call(if same address is assigned
to that local variable) but here I am getting a garbage value(4202616)
when I pointing to same address again. why?
In the second for loop I am getting different values than before
why?
awaiting for ur reply
- Raghu
#include <stdio.h>
int * get_number(int i);
main()
{
int *krrk[5];
int i = 0;
printf(" testing the address of a variable in the function\n i
address in main %u\n", &i);
krrk = get_number(i);
printf("for = i %d address %u and its contents %d\n", i, krrk,
*krrk);
for(i = 1; i < 5; i++)
{
krrk = get_number(i);
printf("for i = %d address %u and its contents %d\n", i, krrk,
*krrk);
printf("for i - 1 = %d address %u and its contents %d\n", i - 1,
krrk[i - 1], *krrk[i - 1]);
}
for( i = 0 ; i < 5; i++)
{
printf(" for i = %d address of %d is %u\n", i, *krrk, krrk);
}
}
int * get_number(int i)
{
return &i;
}
output of this file (I got) is as follows
testing the address of a variable inthe function
i address in main 2280636
for i = 0 address 2280576 and its contents 0
for i = 1 address 2280576 and its contents 1
for i -1 = 0 addrss 2280576 and its contents 4202616
for i = 2 address 2280576 and its contents 2
for i -1 = 1 addrss 2280576 and its contents 4202616
for i = 3 address 2280576 and its contents 3
for i -1 = 2 addrss 2280576 and its contents 4202616
for i = 4 address 2280576 and its contents 4
for i -1 = 3 addrss 2280576 and its contents 4202616
for i = 0 address of 4202660 is 2280576
for i = 1 address of 4202708 is 2280576
for i = 2 address of 4202708 is 2280576
for i = 3 address of 4202708 is 2280576
for i = 4 address of 4202708 is 2280576
my doubt is here I am returning a local variable of a function
and so Its get corrupted for the next call(if same address is assigned
to that local variable) but here I am getting a garbage value(4202616)
when I pointing to same address again. why?
In the second for loop I am getting different values than before
why?
awaiting for ur reply
- Raghu