SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 06/12/2018, 03:37 PM   #1
Aire
Little Clucker
 
Join Date: Mar 2018
Posts: 4
Reputation: 0
Default Issue with sizeof(array) strings and enums

I'm trying to get the size of a string contained in an array (NOT the length of text in the string).

This code:
Code:
#define SALT_LENGTH 65
#define PASSWORD_LENGTH 65

enum e_PlayerInfo {
	Player_Name[MAX_PLAYER_NAME],
	Player_Password[PASSWORD_LENGTH],
	Player_Salt[SALT_LENGTH]
};

new Player[MAX_PLAYERS][e_PlayerInfo];

OnGameModeInit() {
	printf("%i", sizeof(Player[0][Player_Name]));
}
...returns these compile errors:
Code:
 includes\../player/account.pwn(47) : error 001: expected token: "]", but found "-integer value-"
 includes\../player/account.pwn(47) : warning 215: expression has no effect
 includes\../player/account.pwn(47) : error 001: expected token: ";", but found "]"
 includes\../player/account.pwn(47) : error 029: invalid expression, assumed zero
 includes\../player/account.pwn(47) : fatal error 107: too many error messages on one line
I'm not an amazing coder, and I'm just beginning to learn, but this seems odd to me.
As far as I can tell, this should work just fine, but sizeof() refuses to work like this.
I swear I've used it before like this, and I would check, but that project was deleted.

Am I missing something silly, or have I done something horribly wrong?
Any help is appreciated, thanks!

Last edited by Aire; 06/12/2018 at 03:42 PM. Reason: Accidentally pasted wrong compile errors.
Aire is offline   Reply With Quote
Old 06/12/2018, 03:56 PM   #2
coool
Gangsta
 
coool's Avatar
 
Join Date: Feb 2013
Location: Republic of Pakistan
Posts: 586
Reputation: 39
Default Re: Issue with sizeof(array) strings and enums

Maybe you have missed some symbols before that code... Check some lines above the code.
__________________
coool is offline   Reply With Quote
Old 06/12/2018, 04:13 PM   #3
Aire
Little Clucker
 
Join Date: Mar 2018
Posts: 4
Reputation: 0
Default Re: Issue with sizeof(array) strings and enums

While trying to create a new file to show the issue is not with other code, I discovered what I believe to be the problem.

Code:
#include <a_samp>
#include <YSI_Coding\y_hooks>

#define SALT_LENGTH 65
#define PASSWORD_LENGTH 65

enum e_PlayerInfo {
	Player_Name[MAX_PLAYER_NAME],
	Player_Password[PASSWORD_LENGTH],
	Player_Salt[SALT_LENGTH]
};

new Player[MAX_PLAYERS][e_PlayerInfo];

main(){}

OnGameModeInit() {
	printf("%i", sizeof(Player[0][Player_Name]));
}
Upon commenting out the y_hooks include, the compile errors immediately disappeared.
I would test to see if this completely solved the issue, but the script requires y_hooks.

Did I set something up wrong with y_hooks? I'm not sure why it would modify sizeof(), but maybe that's just my ignorance of how it works.
Aire is offline   Reply With Quote
Old 06/12/2018, 05:05 PM   #4
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 15,307
Reputation: 3191
Default Re: Issue with sizeof(array) strings and enums

I donít know why y_hooks altered it, but I donít think this code does what you think. Your enum can also be written as:

Code:
enum {
    Player_Name = 0,
    Player_Password = Player_Name + MAX_PLAYER_NAME,
    Player_Salt = Player_Password + PASSWORD_LENGTH,
    e_PlayerInfo = Player_Salt + SALT_LENGTH,
};
The things in an enum are not arrays, they just have syntax that looks like arrays. However, hopefully now you can see why sizeof (data[Player_Name]) is always just 1.
Y_Less is offline   Reply With Quote
Old 06/12/2018, 05:12 PM   #5
Aire
Little Clucker
 
Join Date: Mar 2018
Posts: 4
Reputation: 0
Default Re: Issue with sizeof(array) strings and enums

Quote:
Originally Posted by Y_Less View Post
I donít know why y_hooks altered it, but I donít think this code does what you think. Your enum can also be written as:

Code:
enum {
    Player_Name = 0,
    Player_Password = Player_Name + MAX_PLAYER_NAME,
    Player_Salt = Player_Password + PASSWORD_LENGTH,
    e_PlayerInfo = Player_Salt + SALT_LENGTH,
};
The things in an enum are not arrays, they just have syntax that looks like arrays. However, hopefully now you can see why sizeof (data[Player_Name]) is always just 1.
I thought I understood enumerators, and this is how I've seen them used (as a 'nametag' to a specific value in an array).
I guess I'll have to re-read some documentation on the subject and try to figure it out.

Thanks for the help!
Aire is offline   Reply With Quote
Old 06/12/2018, 08:15 PM   #6
Aire
Little Clucker
 
Join Date: Mar 2018
Posts: 4
Reputation: 0
Default Re: Issue with sizeof(array) strings and enums

Okay, after pulling most of my hair out and doing a good bit of testing, I've still got no idea what's going on.
String manipulation works on Player[playerid][Player_Name] (strcat, format, etc)...
.. but sizeof() doesn't?

Am I missing something regarding sizeof() itself?
Am I still missing something major regarding enums?

Even if I were completely wrong about how enums work/are used, why would what I'm doing still work except for with sizeof()?
Aire is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings doesn't work in enums Kirollos Scripting Help 2 05/07/2013 09:05 PM
Sizeof 2D Array CoaPsyFactor Scripting Help 16 22/03/2013 11:58 PM
sizeof on strings in enums MP2 Scripting Help 0 18/03/2013 05:14 AM
Enums and Strings adsy Scripting Help 4 07/11/2012 01:01 PM
sizeof 2D array zgintasz Scripting Help 8 05/07/2012 10:35 AM


All times are GMT. The time now is 04:52 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.