• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lonewolff

Passing pointer of a struct to a function

16 posts in this topic

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?

Thanks in advance. :cool:
0

Share this post


Link to post
Share on other sites
Thanks for the reply.

What if the function is in a separate class which doesn't know what type of data [b]*foo [/b]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.
0

Share this post


Link to post
Share on other sites
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;

[size="2"][b]thing->registerData(&t1); \\ sends address to class
[/b]
Within the class;

[b] void registerData(void *pSt1)
{
std::cout<<"Check address within class:"<<pSt1<<"\r\n";
}[/b][/size]

How do I tell the function within the class what the structure member data types were, without declaring the structures globally?
0

Share this post


Link to post
Share on other sites
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?
1

Share this post


Link to post
Share on other sites
That's exactly my problem.
But, I see your point. My brain has been frying just thinking about it :lol:

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. :rolleyes:

I'll get back to you after a couple of tests. :cool:
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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

[code]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
[/code]

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

[code]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);
}
};
[/code]

Probably incorrect syntax there though.
0

Share this post


Link to post
Share on other sites
You could do this:
[code]
// Some header
namespace detail
{
void reallyFrobnicate(void *pointer, int size);
}

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

// Your source file

struct PrivateStruct
{
// ...
};

void example()
{
PrivateStruct s;
frobnicate(&s);
}
[/code]
But if you tell us what you are trying to do, as SenetorBobDole suggests, we can give you concrete advice.
1

Share this post


Link to post
Share on other sites
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).
2

Share this post


Link to post
Share on other sites
Here's a simplified C solution (will work in C++)

[code]
// 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);
[/code]
0

Share this post


Link to post
Share on other sites
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... :-)
0

Share this post


Link to post
Share on other sites
[quote name='SenatorBobDole' timestamp='1304703902' post='4807433']
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... :-)
[/quote]
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.
0

Share this post


Link to post
Share on other sites
[quote name='SenatorBobDole' timestamp='1304703902' post='4807433']
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... :-)
[/quote]

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.
0

Share this post


Link to post
Share on other sites
What are you [b]really[/b] trying to do [b]and why[/b]?

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.
0

Share this post


Link to post
Share on other sites
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
[code]struct foo{
int m;
};

void dofoo( foo* afoo ){
++afoo->m;
}[/code]
foo contains all the data that will be used by dofoo.
All types that you wish to use in dofoo inherits from foo.
[code]struct bar : public foo{
int a;
};[/code]
You can now pass a bar pointer to dofoo
[code]bar mybar;
dofoo( &mybar );[/code]
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0