AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Actual size of array. (https://forums.alliedmods.net/showthread.php?t=216011)

Nanaka 05-15-2013 10:31

Actual size of array.
 
Hello,
Yesterday I started with Amx Mod X and Pawn language.

Let's say that IP in this array is 192.168.1.1 in below example:
Code:

new szIP[128];
get_user_ip(id, szIP, charsmax(szIP));

So, how can I check size of this array?
Sizeof returns 128, but I'm looking for 11, because "192.168.1.1" == 11.

PS. Sorry for my weak english.

Kia 05-15-2013 10:44

Re: Actual size of array.
 
new szIP[128]
->
new szIP[11]

Nanaka 05-15-2013 11:08

Re: Actual size of array.
 
Quote:

Originally Posted by Kia (Post 1952393)
new szIP[128]
->
new szIP[11]

Yeah, it would work if I would know from the beggining size of array, but I don't.
192.168.1.1 was only an example.

Kia 05-15-2013 11:15

Re: Actual size of array.
 
Then make it big enough and use szIP[0] to get the whole array.

Nanaka 05-15-2013 11:40

Re: Actual size of array.
 
Thanks for help.
T/C. Problem solved.

Backstabnoob 05-15-2013 11:50

Re: Actual size of array.
 
Quote:

Originally Posted by Kia (Post 1952405)
Then make it big enough and use szIP[0] to get the whole array.

what? szIP[ 0 ] will return the first character of the string.

To get the current size of the string, use strlen( ).

Kia 05-15-2013 12:20

Re: Actual size of array.
 
Aaaaah, whooops, just write it as it is to get the whole array, sorry. :D

fysiks 05-15-2013 13:09

Re: Actual size of array.
 
Quote:

Originally Posted by Nanaka (Post 1952385)
So, how can I check size of this array?
Sizeof returns 128, but I'm looking for 11, because "192.168.1.1" == 11.

PS. Sorry for my weak english.

The "size of the array" will always be 128. The length of the string is what you want using strlen(). Also, you don't need an array that big because the largest IP you can get from that function is 21 so you should dimension the array with 22.

hleV 05-15-2013 14:33

Re: Actual size of array.
 
In fact, you should do
PHP Code:

new szIP[16];
get_user_ip(idszIPcharsmax(szIP), true); 

This will give you an IP without ":<port>" at the end, and the max length of an IP is 15 symbols.

Nanaka 05-15-2013 22:35

Re: Actual size of array.
 
I did it this way:
PHP Code:

    new szPortIP[64], szIP[32], szPort[6];
get_user_ip(idszPortIPcharsmax(szPortIP));

copyc(szIPcharsmax(szIP), szPortIP':');
copy(szPortcharsmax(szPort), szPortIP[strlen(szIP) + 1]); 



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

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