TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Paul Wankadia
from: Adam Fitzpatrick
date: 1996-10-19 07:54:02
subject: Auto string-length deter

Quoting Paul Wankadia to Frank Adam:

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?

FA>If the string was declared locally yes it would work, but..
FA>The function only gets the pointer to the string, it has no idea about
FA>the length of it, all it knows is that it'll have to put the strng it
FA>gathers up in the function into the address provided.

PW>What would I have to do?  Use strdup() to duplicate the string locally
PW>and then sizeof() that duplicate string perhaps???

since strdup returns a char*, it would have the same result - and the
allocated memory would be strlen(String)+1 (ie, the amount of memory
required by whatever string was stored in String at the time), not 100
(the actual size of the array).
 
FA>So sizeof() will return the length of the pointer.

PW>I seem to get 2...

I presume you're using DOS with the small memory model then. A two byte
pointer is a near pointer, giving you up to 64k to address.

PW>BTW how can I make a pointer to point to a set place in memory (i.e.
PW>segment and offset) ???

You'd need a far pointer. To make far char *st point to 1234:5678, you'd
use st=(far char*)0x12345678. (I haven't needed to do this for a while,
so my memory's a little hazy; it might be char far*).

Your compiler may also provide a function or macro to do this more neatly -
have a look in dos.h.


... MicroSloth: "Bringing you ten-year-old technology, tomorrow, maybe."
--- FMail/Win32 1.22
* Origin: Power BBS - +61-2-858-4114 - Files/Games/EchoMail/Internet (3:711/953)
SEEN-BY: 633/267 270
@PATH: 711/953 410 808 50/99 635/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™.