Archived

This topic is now archived and is closed to further replies.

newbie coder needs help

This topic is 6034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i''m a fairly new coder and i have some questions. i understand what typedefs are, but i cannot see why they would be used for structures and functions. any help would be appreciated. thanks in advance.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Typedefs are used for structures because back in the old days before c++, if you wanted to declare a structure there were basically 2 ways:

struct mystruct {
int some_var;
char id;
};

typedef _mystruct {
int some_var;
char id;
} mystruct;

to use the 1st on, you do this:

struct mystruct ms;

to use the second, however, all you need to do is this:

mystruct ms;

It looks a lot cleaner, but now in c++ it does the typedef automatically. Some people still use typedef since that''s their coding style.

Not sure about functions, though. Someone else has to answer that one.

Share this post


Link to post
Share on other sites
well, you can code one long main() without structs and no functions, and the code will work fine....but...

structs help organize our data, and functions help organize our flow and tasks.

it will be easyer to read, understand and modify a clean code with functions, structs (classes in c++).

good luck.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think he knows what functions are for
Honestly, I don''t see why someone would typedef functions either... Would be interesting though, anyone ??

-JL

Share this post


Link to post
Share on other sites
Well if you are working with callback functions and function pointers typedef is good

Lets say you have a function:

int myFunction();

Now let''s say you want to make a pointer variable that can reference that function. With no typedef you would need to do declare it like this:

int (*)()funcPtr;

That is not a very intuitive declaration.


Now if you typedef the function like:

typedef int Function();

You just made a Function type.
Now if you want to create a pointer variable for it you can just use:

Function* funcPtr;

Which is alot more readable.


Seeya
Krippy

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hmm, yeah makes perfect sense. I never thought of it though, nice idea.

-JL

Share this post


Link to post
Share on other sites
It makes even more sense if you need to cast a function pointer (i.e. if when you''re using pthreads its nice to do it...not necessary, but nice).

consider this:

typedef void (FP)(void *);

void f(int *)
{
...
}

FP a = (FP)f;

Try doing that without typedefs, I don''t even think there is a way. If there is, let me know.


"This album was written, recorded and edited at Gröndal, Stockholm in the year of 2000. At this point in time money still ruled the world. Capitalistic thoughts were wide spread. From the sky filled with the fumes of a billionarie''s cigar to the deepest abyss drenched in nuclear waste. A rich kid was a happy kid, oh..dirty, filthy times. Let this be a reminder."
- Fireside, taken from back of the Elite album

Share this post


Link to post
Share on other sites