Issue with sizeof(array) strings and enums
#1

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

This 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:
Код:
 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!
Reply
#2

Maybe you have missed some symbols before that code... Check some lines above the code.
Reply
#3

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.

Код:
#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.
Reply
#4

Quote:
Originally Posted by ******
Посмотреть сообщение
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:

Код:
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!
Reply
#5

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()?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)