Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
0 registered members (), 18,767 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
passing a struct pointre to a function.. #184985
02/22/08 06:12
02/22/08 06:12
Joined: Aug 2004
Posts: 1,305
New York
PrenceOfDarkness Offline OP
Serious User
PrenceOfDarkness  Offline OP
Serious User

Joined: Aug 2004
Posts: 1,305
New York
Can someone give me a quick piece of code that shows me how to properly hand over a struct pointer to a function? I just can't seem to get it right.


"There is no problem that can't be solved with time and determination." -me
prenceofdarkness for instant messages on AIM.

Looking for a model designer
PLEASE, SEND ME A PRIVATE MESSAGE OR EMAIL IF YOU'RE INTERESTED.
Re: passing a struct pointre to a function.. [Re: PrenceOfDarkness] #184986
02/22/08 08:44
02/22/08 08:44
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline
Member
Fenriswolf  Offline
Member
F

Joined: Jan 2007
Posts: 221
Hello,

here is an example.
It sets a struct pointer to an allocated memory area and initializes the struct.

Code:
void init_mystruct(MYSTRUCT* mystruct) {
mystruct.whatever = ...;
}

...

// dynamically allocate memory for a struct and initialize it
MYSTRUCT* mystruct = malloc(sizeof(MYSTRUCT));
init_mystruct(mystruct);



Re: passing a struct pointre to a function.. [Re: Fenriswolf] #184987
02/22/08 16:06
02/22/08 16:06
Joined: Feb 2008
Posts: 11
Portugal
T
Thiago_7 Offline
Newbie
Thiago_7  Offline
Newbie
T

Joined: Feb 2008
Posts: 11
Portugal
can anyone tell how to do this but instead of mystruct, do it with a array mystruct[n elements]


"i dunno"
Re: passing a struct pointre to a function.. [Re: Thiago_7] #184988
02/22/08 16:12
02/22/08 16:12
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
You can't pass an array by value, but only by reference.
Code:


void f(int* p, int n)
{
int i = 0;
for(i = 0; i < n; i++)
{
p[ i] = 100; // Write over memory area that is where p points to + sizeof(int) * i. That's where the i'th element is at.
}
}
int main()
{
int arr[10];
f(arr, 10); // "arr" means: a pointer to the first element.
}



Re: passing a struct pointre to a function.. [Re: Excessus] #184989
02/22/08 22:28
02/22/08 22:28
Joined: Aug 2004
Posts: 1,305
New York
PrenceOfDarkness Offline OP
Serious User
PrenceOfDarkness  Offline OP
Serious User

Joined: Aug 2004
Posts: 1,305
New York
Why did you do this part:

Code:

MYSTRUCT* mystruct = malloc(sizeof(MYSTRUCT));



Why did you malloc? Will it not work without malloc?


"There is no problem that can't be solved with time and determination." -me
prenceofdarkness for instant messages on AIM.

Looking for a model designer
PLEASE, SEND ME A PRIVATE MESSAGE OR EMAIL IF YOU'RE INTERESTED.
Re: passing a struct pointre to a function.. [Re: PrenceOfDarkness] #184990
02/23/08 15:54
02/23/08 15:54
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
no, you have to allocate memory when using pointers. look up "pointers" in your c language reference. and the correct way to use structs is with the "struct" keyword before your structure identifier, e.g. struct MYSTRUCT s; or struct MYSTRUCT *s = malloc(sizeof(struct MYSTRUCT)), exception herefore is of course using typedefs for declaring structures.

to allocate an array of ten MYSTRUCTS:
Code:
struct MYSTRUCT *structs = malloc(sizeof(struct MYSTRUCT) * 10);



now you can pass this array to function f as follows:
Code:
void f(struct MYSTRUCT *structs, var count);
//...
f(structs, 10);



that is because pointers and arrays have a tight relationship in c. they're basically the same language feature (in plain c, you can replace * by [] nearly at will); the difference in between them is in memory allocation and memory access. declaring and defining var a[3] = {0, 1, 2} gives you the variable a which can't be reassigned but will be converted silently into var *a when passed to a function expecting a pointer as parameter. this conversion will not be done recursively and thus won't work with multidimensional arrays or pointers to pointers.

that's just a brief overview...

joey.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1