TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Joe Kelch
from: Bob Liles
date: 1995-04-11 08:41:00
subject: BORLAND STRINGS 2/2

-> Don't use uninitialized pointers.  The line "char *StrName;" is an
-> uninitialized pointer.  The line "char *StrName[some integer];" is an
-> initialized pointer.  Or you could use
-> {
-> char *StrName;
-> StrName = (char *)malloc(some_integer);
-> }

JK>Only problem is, I don't see how this is going to help you in case of a
JK>string that you simply don't know in advance what size it will be.  It's a
JK>Catch-22; either you must get the string first [gets(string)] and then
JK>malloc(sizeof(string)), which isn't really going to help, or you malloc a
JK>size for your string first, then gets(string) and hope it fits, which isn't
JK>any better than char StrName[some integer].

How about
{
  char *StrName;
  StrName = (char *)malloc(4096);
  gets(StrName);
  StrName = (char *)realloc(StrName, strlen(StrName) + 1);
}
where you initially give it a humongous buffer, fill the buffer, find
the length of the string, add 1 to it to make room for the null byte at
the end, then realloc it down to fit.
___
 * MR/2 2.0 #99 * Internet: bob.liles{at}tpl-bbs.com   RIME: ->7

--- QScan v1.141b / 01-0169
* Origin: The Party Line * Memphis * 901-755-4531 (1:123/5)
SEEN-BY: 105/42 620/243 711/401 409 410 413 430 807 808 809 934 955 712/407
SEEN-BY: 712/515 628 704 713/888 800/1 7877/2809
@PATH: 123/5 4 301 3615/50 396/1 270/101 105/103 42 712/515 711/808 809 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™.