AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Sizeof and charsmax? (https://forums.alliedmods.net/showthread.php?t=135799)

nikhilgupta345 08-19-2010 18:25

Sizeof and charsmax?
 
I got a question

If I were to do this:

Code:

new array[32]
read_argv(1, array, sizeof(array)) //This one uses sizeof

Would it work the same as:

Code:

new array[32]
read_argv(1, array, charsmax(array)) // This one uses charsmax

???

xPaw 08-19-2010 18:26

Re: Sizeof and charsmax?
 
sizeof(array) = 32
charsmax(array) = 31

nikhilgupta345 08-19-2010 18:29

Re: Sizeof and charsmax?
 
Oh, is that the only difference? Or would you use each of them in different cases?

alan_el_more 08-19-2010 18:30

Re: Sizeof and charsmax?
 
charsmax = sizeof - 1

same work :D

fysiks 08-19-2010 19:00

Re: Sizeof and charsmax?
 
Quote:

Originally Posted by nikhilgupta345 (Post 1276086)
Oh, is that the only difference? Or would you use each of them in different cases?

You use charsmax() for strings because the last character in a string must be "^0".

sizeof() can be used in many many places. Depends on what you need.

Bugsy 08-20-2010 00:39

Re: Sizeof and charsmax?
 
sizeof() returns the size of an array. (# of cells)
charsmax() is a macro for sizeof()-1.

string.inc
PHP Code:

#define charsmax(%1) (sizeof(%1)-1) 

As fysiks said, you will almost always use charsmax() when dealing with strings since 1 array cell must be reserved for a null character. In both of your code blocks above it is correct to use charsmax().

When working with arrays and you need to access every cell in the array in a loop or w\e, you would use sizeof().
PHP Code:

new iArray10 ]; //elements are accessed using index 0 through 9

//this loop will reach every element in the array from 0 to 9.

for ( new sizeofiArray ) ; i++ )
//equal to 
for ( new 10 i++ )

//if you used charsmax(), cell index 9 would never be reached.

for ( new charsmaxiArray ) ; i++ )
//equal to 
for ( new i++ ) 


nikhilgupta345 08-20-2010 02:54

Re: Sizeof and charsmax?
 
Thx @all

leo.moraes23 06-24-2014 20:50

Re: Sizeof and charsmax?
 
Hi, sorry I'm opening this post again, is that it is perfect for my doubts. Well Im quite a beginner in programming and Im trying to understand the functions, have doubts about charsmax and sizeof, dont know how to use more actually dont know how to use such as didnt understand a part of this new code "array32 readargv (1, array, charsmax (array))" which means the number 1?. I pray for those who try to help me, I ask you to explain in great detail because I have difficulty in understanding because I dont speak English and use translators to understand, and the translations are bad. Who help me thank you of heart

Flick3rR 06-24-2014 21:12

Re: Sizeof and charsmax?
 
The first parameter (1) has nothing to do with charsmax or sizeof - they are quite different things. This is a paramter for the readargv native itself. It means you read the first arg after the command, or in other words - the first word written. And you store that word into the variable, where you already use charsmax().

leo.moraes23 06-24-2014 21:17

Re: Sizeof and charsmax?
 
Quote:

Originally Posted by Flick3rR (Post 2156942)
The first parameter (1) has nothing to do with charsmax or sizeof - they are quite different things. This is a paramter for the readargv native itself. It means you read the first arg after the command, or in other words - the first word written. And you store that word into the variable, where you already use charsmax().



ahh, so could you explain to me when I put the (1) argument?

If I could say as I would use the for, charsmax and sizeof?


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

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