see the following code-

#include<stdio.h>
#include<conio.h>

struct emp
{   
    int age;
    char name[6];

}*e;

   
void main()
{
    printf("%u\n",&(e->age));
    printf("%u\n",(&(e->age)+0));
    printf("%u\n",(&(e->age)+1));
    printf("%u\n",(&(e->age)+2));
    printf("%u\n\n\n",(&(e->age)+3));

    printf("%u\n",(&(e->name)+0));
    printf("%u\n",(&(e->name)+1));
    printf("%u\n",(&(e->name)+2));
    printf("%u\n",(&(e->name)+3));
    printf("%u\n",(&(e->name)+4));
    printf("%u\n",(&(e->name)+5));
    printf("%u\n\n\n",(&(e->name)+6));

    printf("%u\n",((e->name)+0));
    printf("%u\n",((e->name)+1));
    printf("%u\n",((e->name)+2));
    printf("%u\n",((e->name)+3));
    printf("%u\n",((e->name)+4));
    printf("%u\n",((e->name)+5));
    printf("%u\n\n\n",((e->name)+6));
   
}

OUTPUT-------------------------------
0
0
4
8
12


4
10
16
22
28
34
40


4
5
6
7
8
9
10
----------------------------------
i actually made this program to check if different data members of a structure are indeed stored in contiguous memory locations, and i found this output. i have three questions - -
1. Why are the addresses so small ?
2. i asked the output of the same program from my friend, and he said that his TC++ compiler gave the same result.....how's that possible ? addresses should have been different on two different computers, unless they are not really addresses.........please explain.
3.If I make the pointer 'e' local, that is if i declare it in main(), the output is normal in both VC++ and TC++......(the addresses are large numbers as usual)...why is that so ?

Also, i have an unrelated question. i am a beginner in C/C++(have read C once, and trying to master it now) and my (new)way of studying programming is that when i read a concept, i always test it through a program. Then i try to explain the output by the concept. Then i try to extrapolate the concept, link it with the things i already know, and do some crazy experiments with it, and mostly in case of pointers, i reach a point where i dont know what's happening......will this way of study improve my skills or as my friend says, i shouldn't try to go beyond a level at this point ? (sometimes answers to my questions require knowledge of working of compiler like how it allocates memory,etc....)

change "%u" to "%p" and rerun your program. Also, the way to insure there are no holes in the structure is to use the #pragma pack(1)

#pragma pack(1)
struct emp
{   
    int age;
    char name[6];

} e;
#pragma pack()

int main()
{
    printf("%p\n",&(e.age));
    printf("%p\n",(&(e.age)+0));
    printf("%p\n",(&(e.age)+1));
    printf("%p\n",(&(e.age)+2));
    printf("%p\n\n\n",(&(e.age)+3));

    printf("%p\n",(&(e.name)+0));
    printf("%p\n",(&(e.name)+1));
    printf("%p\n",(&(e.name)+2));
    printf("%p\n",(&(e.name)+3));
    printf("%p\n",(&(e.name)+4));
    printf("%p\n",(&(e.name)+5));
    printf("%p\n\n\n",(&(e.name)+6));

    printf("%p\n",((e.name)+0));
    printf("%p\n",((e.name)+1));
    printf("%p\n",((e.name)+2));
    printf("%p\n",((e.name)+3));
    printf("%p\n",((e.name)+4));
    printf("%p\n",((e.name)+5));
    printf("%p\n\n\n",((e.name)+6));
   
}

I'm not sure, but maybe it is a convention that global variables are allocated memories in the first few slots.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.