AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Get sizeof array in a macro (https://forums.alliedmods.net/showthread.php?t=308246)

shauli 06-12-2018 14:24

Get sizeof array in a macro
 
Hello all.

I've made the following code, which is in my include:
PHP Code:

#define calculateArray(%1,%2) _calculateArray(%1,%2,sizeof %1)

stock _calculateArray(array[], numsize)
{
    
//some unimportant stuff here


Since I can't access directly to the sizeof function inside my include file (and I don't want to manually pass the parameter), I've created a macro that will automatically do that for me.

My problem is that it works fine when I use it like this:
PHP Code:

new array[6];
calculateArray(array, 3); 

But I get an error when I try to use it like this:

PHP Code:

new array[4][6];
calculateArray(array[0], 3); 

Code:

WARNING [273]: expression has no effect
ERROR [273]: expected token: "]", but found "-integer value-"
ERROR [273]: expected token: ";", but found "]"
ERROR [273]: invalid expression, assumed zero

I think that the macro passed "sizeof array[0]" thus making an error. Some workaround for this?
Thanks in advance.

^SmileY 06-12-2018 15:50

Re: Get sizeof array in a macro
 
you can check sizeof array into _calculateArray stock, why you need to pass it?

also what you need to do with new array[4][6]; exactly?

shauli 06-13-2018 05:24

Re: Get sizeof array in a macro
 
Quote:

Originally Posted by ^SmileY (Post 2596600)
you can check sizeof array into _calculateArray stock, why you need to pass it?

also what you need to do with new array[4][6]; exactly?

This stock is inside my include file which I use in many different plugins that are not related to one another. Just a custom-made include.

When I try to use sizeof inside the stock (in the include file) I get this error:
Code:

WARNING [25]: indeterminate array size in "sizeof" expression (symbol "")
About the "array[4][6]" that is just an example. Here's another example that will make more sense:
PHP Code:

new values[MAX_PLAYERS+1][4];
//code......

public someStuff(id)
{
    
calculateArray(values[id], 2);
    
client_print(idprint_chat"%d"values[id][0]);


The purpose of the stock is to allow me to shuffle and "shift" indexes for the array, but that doesn't really matter because I have more stocks like this. Just wanted to know how can I use sizeof inside the stock.

Thank you for your answer! :)

shauli 06-21-2018 17:48

Re: Get sizeof array in a macro
 
Trying again. Does someone has an easy solution?

Natsheh 06-22-2018 04:20

Re: Get sizeof array in a macro
 
#define calculateArray2D(%1,%2,%3) _calculateArray2D(%1,%2,sizeof %3)

stock _calculateArray2D(array[][], num, size)
{
//some unimportant stuff here
}

Example >> calculateArray2D(2Darray[0], 2, 2Darray[])

Am not sure what are you trying to do it really depends on it.

shauli 06-22-2018 10:22

Re: Get sizeof array in a macro
 
Quote:

Originally Posted by Natsheh (Post 2598606)
#define calculateArray2D(%1,%2,%3) _calculateArray2D(%1,%2,sizeof %3)

stock _calculateArray2D(array[][], num, size)
{
//some unimportant stuff here
}

Example >> calculateArray2D(2Darray[0], 2, 2Darray[])

Am not sure what are you trying to do it really depends on it.

I am trying to get the size of an array inside my include file without using an extra parameter. Is it possible?

Since you can't directly use 'sizeof' (gives error) I tried to it with a macro. It works fine, but only when I use 1 dimensional arrays.


All times are GMT -4. The time now is 01:48.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.