TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Joe Kelch
from: Donnie Benners
date: 1995-04-15 09:21:32
subject: Borland Strings

Hello Joe!

Friday April 14 1995 18:52, Joe Kelch wrote to Donnie Benners:

 ->> it will be. If you want to use the pointer, you will need to
 ->> allocate memory fo it before you start filling it with
 ->> information. I like using calloc() because it also clears
 ->> memory, unlike malloc(). Ex: char *StrName; int size, number;
 ->> number = 1; size = 10; StrName = (char *)calloc (number,
 ->> size); This will allocate 10 bytes of memory of type char and
 ->> pass the starting addres to StrName.

 JK> Everyone seems to suggest something like this, but I fail to see
 JK> how this is any different in effect from simply declaring char
 JK> StrName[10] to begin with, which is what I trying NOT to do.  I
 JK> want a VARIABLE length string, which apparently C just doesn't
 JK> let you do!

C will let you do that too. Do this:

char *StrName;

StrName = (char*)malloc (50);
//size to create---------^^

cout << StrName;

You can have variable lengths using pointers, but you need to allocate the
space for it before you can fill it. I use them all the time with
structures and linked lists. Here is an example of reading input into a
buffer then putting it into a string:

char *StrName, buffer[100];

gets(buffer);
StrName = (char*)malloc (strlen (buffer));

cout << StrName;

One of the best books I've seen for beginners is The Turbo C++ Bible from
the Waite Group. It has good examples and would be a very valuable
resource. I still use mine from time to time.

                                                        Donnie

--- GoldED/2 2.42.G0615
* Origin: Benners' BBS -=- New Bern, NC (1:3629/101)
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: 3629/101 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™.