Raised This Month: $32 Target: $400
 8% 

Get sizeof array in a macro


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shauli
Member
Join Date: Jun 2018
Old 06-12-2018 , 14:24   Get sizeof array in a macro
Reply With Quote #1

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.

Last edited by shauli; 06-21-2018 at 17:48.
shauli is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 06-12-2018 , 15:50   Re: Get sizeof array in a macro
Reply With Quote #2

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?
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
shauli
Member
Join Date: Jun 2018
Old 06-13-2018 , 05:24   Re: Get sizeof array in a macro
Reply With Quote #3

Quote:
Originally Posted by ^SmileY View Post
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 is offline
shauli
Member
Join Date: Jun 2018
Old 06-21-2018 , 17:48   Re: Get sizeof array in a macro
Reply With Quote #4

Trying again. Does someone has an easy solution?
shauli is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 06-22-2018 , 04:20   Re: Get sizeof array in a macro
Reply With Quote #5

#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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 06-22-2018 at 04:22.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
shauli
Member
Join Date: Jun 2018
Old 06-22-2018 , 10:22   Re: Get sizeof array in a macro
Reply With Quote #6

Quote:
Originally Posted by Natsheh View Post
#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.
shauli is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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