• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Passing pointer of a struct to a function

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

16 replies to this topic

#1DarkRonin  Members

Posted 05 May 2011 - 10:09 PM

If I have a struct, how can I send the pointer of that struct over to a function so I can access or modify the data within the struct?

Win32 Developer
One Of Them - Martial arts game that is mid development.

#2PrestoChung  Members

Posted 05 May 2011 - 10:17 PM

struct foo{

int m;

};

void dofoo( foo* afoo ){

++afoo->m;

}

foo myfoo;

dofoo( &myfoo );

#3DarkRonin  Members

Posted 05 May 2011 - 10:22 PM

What if the function is in a separate class which doesn't know what type of data *foo is?

The reasoning behind this is that I want to be able to make use of user defined data structures in the class.

Generally, the data structures will be defined after the class headers have been included in to the project. So, initially, the class wont know about what this data is.
Win32 Developer
One Of Them - Martial arts game that is mid development.

#4DarkRonin  Members

Posted 05 May 2011 - 11:32 PM

I worked out how to get the address with a void pointer. But still am not sure how I can access the struct data.

From main program;

thing->registerData(&t1); \\ sends address to class

Within the class;

void registerData(void *pSt1)
{
}

How do I tell the function within the class what the structure member data types were, without declaring the structures globally?
Win32 Developer
One Of Them - Martial arts game that is mid development.

#5yckx  Prime Members

Posted 05 May 2011 - 11:36 PM

If you don't know the parameter's type at the time you write the function, you could use templates. But, if you're passing a struct, that could be any combination of data. How is your function going to know what to do with it?

#6DarkRonin  Members

Posted 06 May 2011 - 12:00 AM

That's exactly my problem.
But, I see your point. My brain has been frying just thinking about it

I am probably thinking about it the wrong way. In greater thought (if there is such a thing) I probably only need to know the address and size of the structure for the purposes that I am after, not the data in-between. Which I now have both.

I'll get back to you after a couple of tests.
Win32 Developer
One Of Them - Martial arts game that is mid development.

#7SenatorBobDole  Members

Posted 06 May 2011 - 12:51 AM

I planned a response that talked about interfaced and virtual classes when I realized something, it won't work with a struct. What is it that you are trying to do, as in what specific problem are you trying to solve with this?

#8lmlPezlml  Members

Posted 06 May 2011 - 01:12 AM

I might have the wrong idea, but have you looked at inheritence? It's poor form to be passing void pointers around. If you're going to have a function that operates on user defined structures, you might want to declare a base class which has the basic properties that you want to operate on, and let users define subclasses of it. This way you can pass your structure around, and if your users want to create a subclass then they can, and add stuff to it, whilst still allowing your function to work as normal on it.

#9Nanoha  Members

Posted 06 May 2011 - 01:29 AM

Only way you can really do it is either to provide a common interface but, as SenetorBobDole points out it won't work with structs - or you include the definitation of each struct in the implementation of the class. Thats pretty much the only option, before you can use it, it must be defined. You could do so with forward declarations but it might not be entirely what you want:

MyStruct.h

struct MyStruct
{
int a;
}

Thing.h
struct MyStruct;

class Thing
{
public:
void RegisterData(MyStruct *myStruct);
};

Thing.cpp
#include "Thing.h"
#include "MyStruct.h"

// We can now use MyStruct


Another option might be to pass in (alongside the struct), a function. The function would be the delegate for adding to the class.

class Thing
{
public:
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]   typedef[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] (*Func)(Thing*, MyStruct*);
[/size]    void RegisterData(MyStruct *myStruc, Func func)
{
func(this, myStruct);
}
};


Probably incorrect syntax there though.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

#10rip-off  Moderators

Posted 06 May 2011 - 02:11 AM

You could do this:
// Some header
namespace detail
{
void reallyFrobnicate(void *pointer, int size);
}

template<typename T>
void frobnicate(T *pointer)
{
detail::reallyFrobnicate(pointer, sizeof(T));
}

struct PrivateStruct
{
// ...
};

void example()
{
PrivateStruct s;
frobnicate(&s);
}

But if you tell us what you are trying to do, as SenetorBobDole suggests, we can give you concrete advice.

#11Aardvajk  Members

Posted 06 May 2011 - 02:13 AM

What's all this "It won't work with structs" stuff about? OP is clearly using C++, not C, since he/she does foo myfoo; rather than struct foo myfoo;
or supplying a typedef.

In C++ the only differences between a struct and a class are the default access and inheritance types (public for structs, private for classes).

#12BeerNutts  Members

Posted 06 May 2011 - 11:44 AM

Here's a simplified C solution (will work in C++)

// in one file
struct
{
int myInt;
int myInt2;
unsigned char myByteArray[12];

} myStruct;

void myFunction(void *myData)
{
myStruct *pMyStruct;

pMyStruct = (myStruct *)myData;

pMyStruct->myInt++;
}

//in a different file

{
SomeDataType myData;
.
.
.

myFunction((void *)&myData);


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#13SenatorBobDole  Members

Posted 06 May 2011 - 11:45 AM

You are right, but they said they were using a struct. I know that a class is essentially a struct with functions, but can a struct inherit from a class? I haven't tried, but even if it can't, I was going to suggest they use a class instead of a struct, since it would work the same, and give them what they want.
But I was waiting for them to give me more info on what they need... :-)

#14NightCreature83  Members

Posted 06 May 2011 - 12:20 PM

You are right, but they said they were using a struct. I know that a class is essentially a struct with functions, but can a struct inherit from a class? I haven't tried, but even if it can't, I was going to suggest they use a class instead of a struct, since it would work the same, and give them what they want.
But I was waiting for them to give me more info on what they need... :-)

In C++ a struct is a class, the compiler generates the same code when it sees "struct" or "class". So yes you can inherit from whatever you want, you can even define a struct as interface.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max

#15Aardvajk  Members

Posted 06 May 2011 - 02:42 PM

You are right, but they said they were using a struct. I know that a class is essentially a struct with functions, but can a struct inherit from a class? I haven't tried, but even if it can't, I was going to suggest they use a class instead of a struct, since it would work the same, and give them what they want.
But I was waiting for them to give me more info on what they need... :-)

With respect, I'd prefer these fora not to descend into guesswork. There are enough sites like that around already.

God, I miss the old GDNet sometimes.

#16Zahlman  Members

Posted 07 May 2011 - 04:39 AM

What are you really trying to do and why?

Do you have to use C++? C++ is a statically typed language. From what I can piece together of what you are doing, statically typed languages are a poor fit for this kind of problem.

#17Wooh  Members

Posted 07 May 2011 - 05:23 AM

I don't think the problem is statically typed language but people trying to solve it in the wrong way.

I think what PrestoChung did was quite good
struct foo{
int m;
};

void dofoo( foo* afoo ){
++afoo->m;
}
foo contains all the data that will be used by dofoo.
All types that you wish to use in dofoo inherits from foo.
struct bar : public foo{
int a;
};
You can now pass a bar pointer to dofoo
bar mybar;
dofoo( &mybar );
dofoo does not have to know that there is a type named bar. All it has to know is the type foo.

When using inheritance it is very common to make use of virtual functions. If you don't know what that is I suggest you learn about it because it allows you to have different functions run for different types.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.