You can help CodeWalrus stay online by donating here. | New CodeWalrus | Old (dark mode) | Old (light) | Discord server

"Declare" a type in C ?

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

0
b/PC, Mac & Vintage Computers publicado por u/Hayleia May 02, 2016, 01:08:39 PM
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".
Last Edit: May 02, 2016, 01:16:30 PM by Hayleia
Inicia sesión o crea una cuenta para dejar un comentario
u/Snektron May 02, 2016, 01:16:57 PM
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.
u/Jean-Baptiste Boric May 02, 2016, 01:23:11 PM
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.
u/Hayleia May 02, 2016, 01:42:58 PM
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
Last Edit: May 02, 2016, 02:00:09 PM by Hayleia
u/Snektron 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.
u/Hayleia 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.
u/novenary May 03, 2016, 09:22:46 AM
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.
u/Snektron May 03, 2016, 12:15:20 PM
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).
u/Adriweb May 03, 2016, 12:18:40 PM
BTW, in a context of a library, to "hide" functions (symbols at least) preventing to link against them, you can declare them static.
Start a Discussion

b/PC, Mac & Vintage Computers

Computer programming discussion and project showcase

132
Topics
Explore Board
Website statistics


MyCalcs | Ticalc.org | Cemetech | Omnimaga | TI-Basic Developer | MaxCoderz | TI-Story | Casiocalc.org | Casiopeia | The Museum of HP Calculators | HPCalc.org | CnCalc.org | Music 2000 Community | TI Education | Casio Education | HP Calcs | NumWorks | SwissMicros | Sharp Calculators
Powered by EzPortal