Typedef and Struct in C and H files

Posted by Leif Andersen on Stack Overflow See other posts from Stack Overflow or by Leif Andersen
Published on 2010-03-20T14:04:20Z Indexed on 2010/03/20 14:11 UTC
Read the original article Hit count: 515

Filed under:
|
|
|

I've been using the following code to create various struct, but only give people outside of the C file a pointer to it. (Yes, I know that they could potentially mess around with it, so it's not entirely like the private keyword in Java, but that's okay with me).

Anyway, I've been using the following code, and I looked at it today, and I'm really surprised that it's actually working, can anyone explain why this is?

In my C file, I create my struct, but don't give it a tag in the typedef namespace:

struct LABall {
    int x;
    int y;
    int radius;
    Vector velocity;
};

And in the H file, I put this:

typedef struct LABall* LABall;

I am obviously using #import "LABall.h" in the c file, but I am NOT using #import "LABall.c" in the header file, as that would defeat the whole purpose of a separate header file. So, why am I able to create a pointer to the LABall* struct in the H file when I haven't actually included it? Does it have something to do with the struct namespace working accross files, even when one file is in no way linked to another?

Thank you.

© Stack Overflow or respective owner

Related posts about struct

Related posts about typedef