TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Roy McNeill
from: Paul Edwards
date: 1996-10-02 23:56:26
subject: Auto string-length determination

PW> after they have declared the variable with a certain length
PW> char String[100] = "";

PW> I tried to automatically determine the maximum string length by using :

PW> int Max_Length = sizeof(String) / sizeof(char);

PW> because it is an array of char.  What am I doing wrong?

RM> You can't ask for the 100 figure that was written in the char
RM> String[100] line, because a C program doesn't remember that number
RM> once it's allocated the ram for String. 

I hope no-one takes this out of context, where it is vaguely true.  :-)

RM> (This is one of the reasons C is fast.) 

I'd hate to have to defend that particular connection for long.

RM> To get the max length, you'll need another parameter to
RM> your function, or a global variable, or a global #define constant.

Or perhaps go:

void fred(char (*x)[100]) { }

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

ie 0.  I'm sure there's a number of reasons why 0 would be inadequate. I
just can't think of them when I'm drunk.  BFN.  Paul. 
@EOT:

---
* Origin: X (3:711/934.9)

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™.