Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/20/24 01:28
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (7th_zorro), 793 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Array size #170865
12/03/07 19:59
12/03/07 19:59
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
Hello,

how can I get the size of an array?

I have tried the following:

var my_array[7];
...
size = sizeof(my_array) / sizeof(my_array[0]);

However, sizeof(my_array[0]) leads to an engine crash.

Thanks

Re: Array size [Re: Fenriswolf] #170866
12/04/07 08:43
12/04/07 08:43
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
What do you mean with size, amount of indexes? Then what are you trying to achieve with sizeof(my_array[0])?

Furthermore, this is what the manual says about sizeof():
Quote:


The sizeof() function returns the size of a variable or a struct in bytes. This can be used to initialize structs through the zero macro that is defined in include\acknex.h





If you want to have the amount of indexes an array contains, you have to make your own function because as far as I'm aware, lite-c does not have such a function yet.

Not tested code which is inevitebly wrong, but it's about the concept:
Code:

int getArrayLength(var array) {
int n = 0;
while (array[n]){n++}
return n;
}



Last edited by Joozey; 12/04/07 08:44.

Click and join the 3dgs irc community!
Room: #3dgs
Re: Array size [Re: Joozey] #170867
12/04/07 09:52
12/04/07 09:52
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
Hi,

Quote:

What do you mean with size, amount of indexes?



Yes, I've meant the number of elements.

Quote:

Then what are you trying to achieve with sizeof(my_array[0])?



I wanted to calculate the size of a single array element in bytes.
Dividing the array size in bytes by the element size in bytes should result the array's length in elements, if I'm not mistaken.
If arrays are handled as pointers this would not work, though.


Your code works fine, thank you!
However, the last element of the array has to be NULL; otherwise a wrong value is returned:

var my_array[3] = { 1, 2 } // works
var my_array[3] = { 1, 2, 3 } // does not work

Re: Array size [Re: Fenriswolf] #170868
12/04/07 17:47
12/04/07 17:47
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Quote:


Dividing the array size in bytes by the element size in bytes should result the array's length in elements, if I'm not mistaken.





Good point, hadn't thought of that idea. But I think arrays are indeed handled as pointers. I don't see a good solution for this at the moment, you'll just have to add NULL in every array I guess, or somehow store the index of each array at game start.


Click and join the 3dgs irc community!
Room: #3dgs
Re: Array size [Re: Joozey] #170869
12/04/07 18:06
12/04/07 18:06
Joined: Dec 2006
Posts: 78
Nevada, USA
Futurulus Offline
Junior Member
Futurulus  Offline
Junior Member

Joined: Dec 2006
Posts: 78
Nevada, USA
Quote:

I think arrays are indeed handled as pointers.



Yeah -- I don't know about in Lite-C in this case, but in a conforming C compiler
Code:
sizeof(MyArray) / sizeof(MyArray[0])

would probably return 1 on a 32-bit system, because sizeof(a pointer) = 4 and sizeof(a var) = 4.

Re: Array size [Re: Futurulus] #170870
12/05/07 08:08
12/05/07 08:08
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
Thank you both for your help!
Probably I will either use the getArrayLength function or pass the array length as a parameter.
These are the best solutions I can think of at the moment.


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

Gamestudio download | chip programmers | 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