#14864 - DekuTree64 - Tue Jan 13, 2004 9:02 pm
There's probably a simple solution to this, but is there any way to declare a pointer to a struct in itself in C? Like
Code: |
typedef struct tFoo
{
tFoo *bar;
} Foo, *pFoo; |
That works fine in MSVC++ for windows, but not with GCC. Is it a C++-only feature, or just a compiler difference letting me get away with something non-standard? I've tried using Foo * and pFoo, but as logic would suggest, only using the original type definition works even in MSVC++, because the others haven't been declared yet.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku
#14865 - Miked0801 - Tue Jan 13, 2004 9:42 pm
Forward declare the struct if you're getting into trouble.
Code: |
struct tFoo;
typedef struct tFoo
{
tFoo *bar;
} Foo, *pFoo;
|
#14867 - DekuTree64 - Tue Jan 13, 2004 9:50 pm
Nope, still says there's a syntax error before tFoo *bar;. Has anyone actually gotten it to work in DevKitAdv? I'm sure it's possible, but I can't very well do it if it keeps giving me errors. I can make it work just fine using a void* and just cast it when actually doing things, it's just a hassle to do.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku
#14868 - Miked0801 - Tue Jan 13, 2004 10:37 pm
Oh. I see what's wrong. You can't use the typedef declaration before it's defined.
Do this:
Code: |
typedef struct tFoo
{
struct tFoo *bar;
} Foo, *pFoo;
|
And yes, I've gotten it to work just fine (used for linked lists and such)
#14869 - poslundc - Tue Jan 13, 2004 10:40 pm
You don't have to use separate names for the struct and the typedef if you don't want to. (Most people rarely do, since once you typedef a struct there is no practical reason to use the struct keyword with it anymore.)
Code: |
typedef struct Foo
{
struct Foo *f;
}
Foo; |
Dan.
#14909 - MumblyJoe - Wed Jan 14, 2004 6:55 pm
Just from a C++ perspective, there is no reason this wont work:
so if you are using C++, forget all that typedef shit, if you are using C, well too bad... :P
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!
#14910 - DekuTree64 - Wed Jan 14, 2004 7:06 pm
I love C, much less to worry about than C++. And besides, Dan's way worked (thanks!), so I'm happily on my way to an object-oriented battle system. Admittedly, C++ may be a better choice in this case, but I may be rewriting some of the code in ASM in the future, and C translates quite nicely.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku
#14915 - Maddox - Wed Jan 14, 2004 7:56 pm
MumblyChoad,
C++ on the GBA sucks ass. So quit giving crap advice to poor slobs when they ask about structs in structs.
And anyway, a class member variable of type pointer-to-class is just bad form in C++.
_________________
You probably suck. I hope you're is not a game programmer.
#14917 - jma - Wed Jan 14, 2004 8:12 pm
Maddox wrote: |
And anyway, a class member variable of type pointer-to-class is just bad form in C++. |
Where do you get this idea from? How do you think linked lists, queues and stacks are made?
Jeff
_________________
massung@gmail.com
http://www.retrobyte.org
#14919 - torne - Wed Jan 14, 2004 8:19 pm
Also, C++ on the GBA works perfectly.
#14925 - bomberman - Wed Jan 14, 2004 8:33 pm
maybe he just has problems with C++ in general and not only on GBA...
#14948 - MumblyJoe - Thu Jan 15, 2004 2:54 am
Maddox wrote: |
MumblyChoad,
C++ on the GBA sucks ass. So quit giving crap advice to poor slobs when they ask about structs in structs.
And anyway, a class member variable of type pointer-to-class is just bad form in C++. |
Sounds like someone has some daddy issues.
C++ on the GBA is just fine, and anyone who truly understands what the compiler does to C++ code knows how to avoid any pitfalls that may arise. Also a pointer to the same class in C++ is bad form if it is public, and much less bad form than C if you use accessor functions. Note that nothing was public in the example I gave, because it was an example.
Back to the actual topic though and forgetting about flaming jerks, I think why your original code may have worked in MSVC++ is because you may have been using the default .cpp filename, which will still compile any C code, but may complain more about casting etc (which is good, Bruce Eckel recommends doing this anyway) and will allow you to use the keyword stuct like the keyword class.
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!
#14978 - tepples - Thu Jan 15, 2004 5:02 pm
MumblyJoe wrote: |
you may have been using the default .cpp filename, which will still compile any C code, but may complain more about casting etc (which is good, Bruce Eckel recommends doing this anyway) |
Not necessarily. Though C99 has absorbed quite a few of C++'s improvements to C, I can think of several things that I'd find useful in C99 that C++ lacks.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#14980 - torne - Thu Jan 15, 2004 5:15 pm
Though most sensible compilers use the extensions in C++ and C99 at the same time (except where they conflict).
#14990 - animension - Thu Jan 15, 2004 7:53 pm
Quote: |
maybe he just has problems with C++ in general and not only on GBA...
Quote: |
MumblyChoad,
C++ on the GBA sucks ass. So quit giving crap advice to poor slobs when they ask about structs in structs.
And anyway, a class member variable of type pointer-to-class is just bad form in C++.
|
|
I think he has a problem with everything, many teenagers do. He obviously doesn't know what he's talking about, or he'd know that any dynamic data structure like lists, queues, stacks, heaps, maps, etc, couldn't exist if what he said is true.
_________________
"Beer is proof that God loves us and wants us to be happy."
-- Benjamin Franklin
#15002 - tepples - Thu Jan 15, 2004 11:22 pm
animension wrote: |
Quote: | And anyway, a class member variable of type pointer-to-class is just bad form in C++. |
I think he has a problem with everything, many teenagers do. He obviously doesn't know what he's talking about, or he'd know that any dynamic data structure like lists, queues, stacks, heaps, maps, etc, couldn't exist if what he said is true. |
I read it and inferred that he found it better to use STL than to implement one's own data structures.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#15020 - MumblyJoe - Fri Jan 16, 2004 4:21 am
Good point tepples. Know of any good sites/books you can recomend for learning C99, I sort of ignored it because it's still not how I like to do things...
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!