extern and struct size question

I received, among others, the following two questions at some tests here at my university, but I’m not sure about the right answers. The questions are for the C language, not C++.

Q1 Assuming that addresses are represented on 4 bytes, how much memory space is reserved by the following declarations:
1
2
3
4
5
extern long count;
struct node {
    long key;
    struct node *next;
};

A 0 bytes; B 4 bytes; C 8 bytes; D 12 bytes;

Q2 Assuming that addresses are represented on 4 bytes, how much memory space is reserved by the following declarations:
1
2
3
4
5
extern long count;
struct node {
    long key;
    struct node *next;
} val;

A 0 bytes; B 4 bytes; C 8 bytes; D 12 bytes;

Which are the right answers and why?
Thanks in advance!
Last edited on
Remember that extern just tells the compiler that something already exists elsewhere.

Also remember that a type declaration doesn't actually reserve any memory -- all it does is tell the compiler how to organize (or view) some piece of memory.

The only time memory is reserved is when actually declaring variables.

Hope this helps.
Let's see...

For Q1, extern takes 0 bytes and struct also 0 bytes, so right answer A 0 bytes.

For Q2, extern takes 0 bytes, but struct, being declared there as val, takes 4 bytes for the long variable and 4 bytes for the pointer to struct variable, so right answer C 8 bytes.

If I answered correctly that means that I got it. :D

Thanks once again!
And last but not least, I liked the way you managed to reply, you didn't provided me directly the answers, but guided me and let me think about which ones are correct.
Last edited on
Topic archived. No new replies allowed.