* WalrusIRC

You need to have 5 posts and not be part of restricted usergroups in order to use the WalrusIRC embedded shoutbox. However, you can also access our IRC channel called #CodeWalrus via EFnet.

Author Topic: "Declare" a type in C ?  (Read 1199 times)

0 Members and 1 Guest are viewing this topic.

Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
"Declare" a type in C ?
« on: 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.
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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
Code: [Select]
typeexists newTypeexcept 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 »



Offline Snektron

  • Lvl 69 Russian Snake
  • Super User
  • Join Date: Dec 2014
  • Location: Netherlands
  • Posts: 3165
  • Post Rating Ratio: +32/-0
  • SSSssssss.....
    • RobinDeWalvis
    • Kzyrox
    • RobinDeWalvis
    • quantuminfinity
  • Gender: Male
Re: "Declare" a type in C ?
« Reply #1 on: 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.
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Offline Jean-Baptiste Boric

  • Full User
  • Join Date: Jan 2016
  • Location:
  • Posts: 25
  • Post Rating Ratio: +2/-0
Re: "Declare" a type in C ?
« Reply #2 on: May 02, 2016, 01:23:11 pm »
You want to declare an opaque pointer, like how FILE is declared in stdio.h :

Code: [Select]
typedef struct _IO_FILE FILE;
Then you'd define struct _IO_FILE somewhere else :

Code: [Select]
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.

Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
Re: "Declare" a type in C ?
« Reply #3 on: 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 »

Offline Snektron

  • Lvl 69 Russian Snake
  • Super User
  • Join Date: Dec 2014
  • Location: Netherlands
  • Posts: 3165
  • Post Rating Ratio: +32/-0
  • SSSssssss.....
    • RobinDeWalvis
    • Kzyrox
    • RobinDeWalvis
    • quantuminfinity
  • Gender: Male
Re: "Declare" a type in C ?
« Reply #4 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.
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
Re: "Declare" a type in C ?
« Reply #5 on: May 03, 2016, 08:58:38 am »
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.

Offline Streetwalrus

  • Professional slacker
  • Super User
  • Original 5
  • Join Date: Nov 2014
  • Location: Israel
  • Posts: 2903
  • Post Rating Ratio: +20/-0
  • ƎW∀⅁ ƎH⊥
  • Gender: Male
Re: "Declare" a type in C ?
« Reply #6 on: 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.
  • Calculators owned: TI-80, HP 40G, TI-84 Plus rev G (yay 128k RAM), TI-83 Plus Silver Edition (broken LCD), TI-82 Stats.fr (black), TI-Nspire CX rev C (yay Nlaunchy), TI-83+ SE ViewScreen



Offline Snektron

  • Lvl 69 Russian Snake
  • Super User
  • Join Date: Dec 2014
  • Location: Netherlands
  • Posts: 3165
  • Post Rating Ratio: +32/-0
  • SSSssssss.....
    • RobinDeWalvis
    • Kzyrox
    • RobinDeWalvis
    • quantuminfinity
  • Gender: Male
Re: "Declare" a type in C ?
« Reply #7 on: May 03, 2016, 12:15:20 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...


Offline Adriweb

  • Full User
  • Join Date: Jan 2015
  • Location: France & Canada
  • Posts: 423
  • Post Rating Ratio: +7/-1
    • Adriweb
    • @Adriweb
    • Adriweb
    • @UC-UIrbk5SuaUCvnV6SQvt-Q
    • /u/Adriweb
    • Adriweb
    • TI-Planet.org
  • Gender: Male
Re: "Declare" a type in C ?
« Reply #8 on: 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.
  • 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

 


You can also use the following HTML or bulletin board code to share it on your page or forum signature!


Also do not forget to check our affiliates below.
Planet Casio TI-Planet Calc.news BroniesQC BosaikNet Velocity Games