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[] = "hello"; //string length 5 array size 6, hello[5] == 0x0000.<br>char dex[] = "dex"; //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
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 <AfxTempl.h><br><br>CArray<CString,LPCSTR> 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 "string"<br>#include "vector"<br><br>using namespace std;<br><br>vector<string> astrMyStringsArray;<br><br>You can heck out the STL tutorial on the MSDN about this.<br><br>All the Best,<br><br>Ami Tavory
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.