Raised This Month: $ Target: $400
 0% 

Sizeof and charsmax?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 08-19-2010 , 18:25   Sizeof and charsmax?
Reply With Quote #1

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
???
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 08-19-2010 , 18:26   Re: Sizeof and charsmax?
Reply With Quote #2

sizeof(array) = 32
charsmax(array) = 31
__________________
xPaw is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 08-19-2010 , 18:29   Re: Sizeof and charsmax?
Reply With Quote #3

Oh, is that the only difference? Or would you use each of them in different cases?
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
alan_el_more
Veteran Member
Join Date: Jul 2008
Location: amxmodx-es.com
Old 08-19-2010 , 18:30   Re: Sizeof and charsmax?
Reply With Quote #4

charsmax = sizeof - 1

same work
__________________
alan_el_more is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-19-2010 , 19:00   Re: Sizeof and charsmax?
Reply With Quote #5

Quote:
Originally Posted by nikhilgupta345 View Post
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.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-20-2010 , 00:39   Re: Sizeof and charsmax?
Reply With Quote #6

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++ ) 
__________________

Last edited by Bugsy; 08-20-2010 at 00:43.
Bugsy is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 08-20-2010 , 02:54   Re: Sizeof and charsmax?
Reply With Quote #7

Thx @all
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
leo.moraes23
Junior Member
Join Date: Jul 2012
Old 06-24-2014 , 20:50   Re: Sizeof and charsmax?
Reply With Quote #8

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
leo.moraes23 is offline
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 06-24-2014 , 21:12   Re: Sizeof and charsmax?
Reply With Quote #9

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().
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
leo.moraes23
Junior Member
Join Date: Jul 2012
Old 06-24-2014 , 21:17   Re: Sizeof and charsmax?
Reply With Quote #10

Quote:
Originally Posted by Flick3rR View Post
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?

Last edited by leo.moraes23; 06-24-2014 at 21:21.
leo.moraes23 is offline
Reply



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 22:01.


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