| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | (Im)proper use of string |
GW> It is legal C, but as it is modifying the original string, it is not GW> doing the same as a BASIC LEFT$[], which extracts a substring without GW> modifying the original. Yeah, there is that. I'm pretty sure it's not ANSI-standard, but my compiler has a memcpy() function, and I'd assume many other ones have something similar. If not, it wouldn't be too hard to write one (although it would likely not execute as fast as one included with the compiler, but...) Then he could just malloc() a chunk of memory, and copy x bytes from the source string into it. If writing his own memory-copy routine, he could also include a check for 0, so he wouldn't copy more beyond the end of the string- though as long as the allocated block was large enough (as it would be if it was simply the same size as the source block), this wouldn't actually cause any problems, it would merely slow things down a tad. GW> Sizeof does not give the length of a string, and cannot give the GW> length of malloc'd memory. That is why there is a strlen() function in GW> the standard library, but it's no use here. Yup, my mistake is pointed out again. I was meaning strlen(), even though I wrote sizeof(). As for strlen(), I'd assume it just takes the pointer to the source string, initializes a counter variable, then loops through, incrementing the counter by 1 and breaking out of the loop if string[counter]=0. It would then return the last counter variable, which would be the length of the string. If this is how it works (and if it isn't how it works, and you know how it does work, please let me know), it should work with malloc'd memory just fine. Of course, as I pointed out in the previous message, simply 'remembering' the number of bytes that were allocated (in the destination string, if he is doing the 'extract sub-string without modifying original string) would likely be preferable to a call to strlen() each time, even if it does work. ... "You know, it tastes kind of like chicken to me." -J.C. Peter, Brujah ___ Blue Wave/DOS v2.30 [NR] --- Maximus 2.01wb* Origin: Basic'ly Computers: Mooo-ing Right Along. (1:153/9) SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728 SEEN-BY: 639/252 670/213 218 @PATH: 153/104 2 716 7715 140/1 270/101 396/1 633/260 635/506 728 633/267 |
|
| 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™.