I have this simple program:
#include <stdio.h>
struct time
{
int hours ;
int minutes ;
int seconds ;
} t[2] = {1,2,3,4,5,6};
int main() {
struct time *tt = t;
printf("%d\n", (*tt));
printf("%d", *(tt+1));
}
Now the expected output should be:
1
4
But the output I am getting is 2 exactly same memory addresses. I sent this program to other people and some are having the expected output but some aren't. I reckon this is an issue with version of C or GCC. Is it resolvable ? Is there any explanation as to why this is happening in my version of C?
The most weird thing is that even after I have dereferenced the pointer, it still printing some address. How is this possible?
On the other hand if I change the program to this:
$include <stdio.h>
struct time
{
int hours ;
int minutes ;
int seconds ;
} t[2] = {1,2,3,4,5,6};
int main() {
struct time *tt = t;
printf("%d\n", (*tt).hours);
printf("%d", (*(tt+1)).hours);
}
It prints the expected output.
What is the explanation for this behaviour? Thanks.