TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Paul Wankadia
from: Roy McNeill
date: 1996-10-09 22:27:52
subject: Auto string-length determination

Hello Paul



 PW> Oh.  What if I use strdup() locally to create another string?



You can get the length of the current string anytime with strlen(),

and this will return the same value on the new string produced with

strdup(). Neither technique will tell you anything about the [100]

that was declared before your function was called - if you want

that figure, you'll have to pass that number as a parameter to the

function, or declare it globally somehow.



 RM> Exactly why do you need the max length? Why can't you work with

 RM> the length of the current string?



 PW> Eh? I'm writing my own custom Get_Input() function and I'd like to

 PW> cut down the number of parameters that it takes -- especially the

 PW> one that passes the length of String... The length may vary from

 PW> program to program, so I have to account for that :)



Why? Let the user pass the string to you, find its length with

strlen(), and use alloc(), malloc(), new (in C++), or their

relatives, to allocate any memory whose size depends on this

length. (Don't forget to dispose of this memory when you're

finished with it - use free() or delete or whatever. A polluted

heap can stuff up your program much later, and is a real bugger to

track down.)



Cheers



--- PPoint 1.88


* Origin: Silicon Heaven (3:711/934.16)
SEEN-BY: 711/808 934
@PATH: 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.