Join us on Discord!
You can help CodeWalrus stay online by donating here.

"Declare" a type in C ?

Started by Hayleia, May 02, 2016, 01:08:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Hayleia

Well, that title doesn't make any sense, so I'll explain obviously.

Basically, in C, you can have a function.

int min(int a, int b)
{
    return a<b ? a : b;
}


Great, but that's in the .c, in the .h you only have this:

int min(int a, int b);

Which means "that function exists, you can use it, and since it is probably documented you don't need to have its code to know what it does so just use it and don't look at the code".

Now, I'd like to do the same with a type. Say I have a typedef as follows:

typedef struct {
    int idk;
    float dunno;
} newType;


And I don't really want to give the details of it in the .h, just say that it exists and is usable, like a
typeexists newType
except that this probably doesn't work.

So, is it possible ? If so, how do I do it ?

edit Can't find anything on google. When I write "declare type" I get discussions about typedef, which "creates" a type, not says "it exists but we still don't know what it is".

Snektron

Types are declared in the header file :)

Maybe you can do "extern newType" in your header too, and declare it in the source file, but i think its better to just keep them in the header.
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Jean-Baptiste Boric

You want to declare an opaque pointer, like how FILE is declared in stdio.h :

typedef struct _IO_FILE FILE;

Then you'd define struct _IO_FILE somewhere else :


struct _IO_FILE {
  int _flags;           /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;   /* Current read pointer */
  char* _IO_read_end;   /* End of get area. */
  char* _IO_read_base;  /* Start of putback+get area. */
[...]
};


More details at http://stackoverflow.com/questions/5299261/c-typedefed-opaque-pointer.

Hayleia

#3
Thanks both of you :)

@Jean-Baptiste Boric's solution worked but now doxygen tells me (adapted to this example here) "warning: Compound _IO_FILE is not documented" -.-
edit fixed with @cond and @endcond

And @Cumred_Snektron's solution for some reason created problems where stuff were like "dereferencing in something that is not a struct" even though I obviously don't dereference anything in the .h (no code) and the type is defined as a structure in the .c... Maybe I failed something somewhere.

And yeah, I know I should theoretically declare types in the .h, but actually, I'd like a .h that is independant from anything (like "well I have these types") instead of a .h that declares types that depend on other types that come from libs (like "well I have these types and I use these libs and there is no way you could adapt the code using other libs (or no libs) if you wanted to", even though that should be possible). Also, I'd like the user not to use what's in that struct, to avoid problems, and to let the program use these variables the way they are intended to without interference :P

Snektron

I don't think there's a way to prevent that in C :/ Except if you declare the struct inside a source file( so not in the header), but then you can only use it in that sourcefile too.
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Hayleia

Quote from: Cumred_Snektron on May 02, 2016, 06:27:21 PM
I don't think there's a way to prevent that in C :/ Except if you declare the struct inside a source file( so not in the header), but then you can only use it in that sourcefile too.
Yeah, and I basically only use it in the sourcefile... but the problem is that functions declared in the header use that type so that's why I have to declare the type in the header somehow.

novenary

extern is not made for declaring types, you can actually do forward declarations with typedef but last time I've tried it yells about incomplete type declarations, Jean-Baptiste's solution seems to be the complete version of that.

Snektron

Quote from: Hayleia on May 03, 2016, 08:58:38 AM
Quote from: Cumred_Snektron on May 02, 2016, 06:27:21 PM
I don't think there's a way to prevent that in C :/ Except if you declare the struct inside a source file( so not in the header), but then you can only use it in that sourcefile too.
Yeah, and I basically only use it in the sourcefile... but the problem is that functions declared in the header use that type so that's why I have to declare the type in the header somehow.
Then can't you make those functions private? (As in, declare them in the source file too).
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Adriweb

BTW, in a context of a library, to "hide" functions (symbols at least) preventing to link against them, you can declare them static.
  • Calculators owned: TI-Nspire CX CAS, TI-Nspire CX, TI-Nspire CAS (x3), TI-Nspire (x2), TI-Nspire CM-C CAS, TI-Nspire CAS+, TI-80, TI-82 Stats.fr, TI-82 Plus, TI-83 Plus, TI-83 Plus.fr USB, TI-84+, TI-84+ Pocket SE, TI-84+ C Silver Edition, TI-84 Plus CE, TI-89 Titanium, TI-86, TI-Voyage 200, TI-Collège Plus, TI-Collège Plus Solaire, 3 HP, some Casios
Co-founder & co-administrator of TI-Planet and Inspired-Lua

Powered by EzPortal