×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

String array. How?

String array. How?

String array. How?

(OP)
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.

dex

RE: String array. How?

normally you can do it like this

char mystring[5];

that'll create a charater array of 5 characters (including the null terminator)

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.

Karl
kb244@kb244.8m.com

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)

RE: String array. How?

Dear dex,

Array's of 'char' are very different in C/C++ than in Pascal.

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.

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.

char hello[] = "hello"; //string length 5 array size 6, hello[5] == 0x0000.
char dex[] = "dex"; //string length 3 array size 4, dex[3] == 0x0000.

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.

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.

MFC has CString
STL has wstring, string and rope

Good luck
-pete

RE: String array. How?

(OP)
Thanks to you all but I figured my problem out. Anyway your help is greatly appreciated.

Regards...dex

RE: String array. How?

Hello,

I would like to add to Karl's reply.
You could define, if you wish to use MFC, an array like this
#include <AfxTempl.h>

CArray<CString,LPCSTR> astrMyStringsArray;

.

If you wan't your code to run without MFC, you could write the following:

#pragma warning(disable: 4786)

#include "string"
#include "vector"

using namespace std;

vector<string> astrMyStringsArray;

You can heck out the STL tutorial on the MSDN about this.

All the Best,

Ami Tavory

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close