Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String array. How? 1

Status
Not open for further replies.

dex1123

Programmer
May 9, 2000
35
BG
I am a Pascal programmer and I've just moved to C++. I was wondering how to create a string (char *) type array. I guess I am having problems with pointer references. Any help is welcome. Thanks in advance.<br><br>dex
 
normally you can do it like this<br><br>char mystring[5];<br><br>that'll create a charater array of 5 characters (including the null terminator)<br><br>also depending on which classes you have, there is also a String class, which acts like an actual string, to the extent that you dont have to worry too much about going beyound the bounds of the array. thing its called CString. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Dear dex,<br><br>Array's of 'char' are very different in C/C++ than in Pascal. <br><br>In Pascal the length of the string is stored at the front of the array followed by the characters themselves. All of the string manipulation functions know this of course.<br><br>In C/C++ the length is not stored instead the string is terminated with a null character 0x0000, at any point in the array so that the length of the 'string' will not be the same as the length of the array. Most of the string manipulation functions work by looking for the first null character from the start of the array.<br><br>char hello[] = &quot;hello&quot;; //string length 5 array size 6, hello[5] == 0x0000.<br>char dex[] = &quot;dex&quot;; //string length 3 array size 4, dex[3] == 0x0000.<br><br>char junk[5]; // not initialized so if none of the bytes happen to be 0x0000 any string function will crash not finding a null until it moves beyond the allocated space.<br><br>Without a decent understanding of this you will have problems working the char arrays. If you use C++ String classes you will find it much easier.<br><br>MFC has CString<br>STL has wstring, string and rope<br><br>Good luck<br>-pete
 
Thanks to you all but I figured my problem out. Anyway your help is greatly appreciated.<br><br>Regards...dex
 
Hello,<br><br>I would like to add to Karl's reply.<br>You could define, if you wish to use MFC, an array like this<br>#include &lt;AfxTempl.h&gt;<br><br>CArray&lt;CString,LPCSTR&gt; astrMyStringsArray;<br><br>. <br><br>If you wan't your code to run without MFC, you could write the following:<br><br>#pragma warning(disable: 4786)<br><br>#include &quot;string&quot;<br>#include &quot;vector&quot;<br><br>using namespace std;<br><br>vector&lt;string&gt; astrMyStringsArray;<br><br>You can heck out the STL tutorial on the MSDN about this.<br><br>All the Best,<br><br>Ami Tavory
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top