Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C] static types?

Started by DevFred Oct 24, 2009 at 4:16 PM 6 replies 900+ views
Original Post
DevFred
DevFred
I have a data structure that uses a recursive node type. Can I hide this type from other translation units? static struct node... does not work.
magic_man
magic_man
You could forward declare in the header and in the source file define it with the functions in the same file using it.
"You insulted me!" I did not say that in the private message Tom Sloper!
LessBread
LessBread
In C the keyword "static" modifies the variable instance not the variable type.

Preserves variable value to survive after its scope ends.

If you want to hide a type from other translation units, don't put the definition of that type inside any header that you include in other translation units. If you want to limit that type definition to a single translation unit, declare it in the ".c" file of that unit, at the top of the file or at least before the point where it's used. Avoid using that type as an argument to any function that you want to export from that translation unit.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
SiCrane
SiCrane
Why does it even matter? Types don't have linkage in C.
DevFred
DevFred
Quote:
Original post by SiCrane
Why does it even matter? Types don't have linkage in C.

Oh... well, nevermind then :)
DevFred
DevFred
struct node{    struct node *next;    int size;};#define HEAP_SIZE 1000static struct node heap[HEAP_SIZE];

As I understand it, static objects are zero-initialized in C*. Does that mean that heap.next will be the null pointer, or will it be sizeof(struct node*) zero bytes? Will (heap.next == 0) yield 1 or 0?

*Yes I know, globals are always static, even without the static :-) I just don't want heap to escape the translation unit.
SiCrane
SiCrane
Pointers with static storage duration without an explicit initializer will be initialized to the null pointer.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.