Link to home
Start Free TrialLog in
Avatar of adam8
adam8

asked on

easy question

hi,
in visual basic the String data type can store a lot of characters.
in c++ the char datatype can only store one character.
what datatype is the same as the STRING in visual basic
Avatar of bbousquet
bbousquet
Flag of Canada image

You have a number of options:

1. use an array of chars - in this case, 30 characters:
char somestring[30];

2. use the standard template library's (STL) 'string' type

3. use the MFC 'CString' class - if you're using Windows, that is


Avatar of RONSLOW
RONSLOW

that looks like an answer bbousquet :-)

BTW: when using an array of char's, you need to remember the C wants an ascii 0 character (nul) at the end of the string.

So if you want a variable that can hold a 30 character string, make the array at least 31 char's long (to take into account the nul terminator.

string (and MFC CString) don't need to have their size pre-determined .. they will grow and shrink as required (like VB strings do).
sorry if i'm saying something that already has been said

1)char str[10];
2)char *str;
3)String class that in string.h
4)String class in STL
5)Cstring in MFC
6)AnsiString in C++Builder

if you know the size of your string,
you need to allocate the necessary memory to a character pointer:

char *ptr;

ptr=(char *)malloc(string_length+1);

the plus 1 is for the null character that marks the end of a string.
ASKER CERTIFIED SOLUTION
Avatar of bbousquet
bbousquet
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adam8

ASKER

i have just started using it. it is fine to tell me what to use but i don't even know how to use arrays yet.

how can i user the MFC CString class.
i just need something that will work on Windows 95, 98 NT and the main OS's
The MFC CString class is specific to Windows. If you want your code to be portable to other platforms, you can use either a good old char array or the STL's string type (standard template libraries are usually available for most platforms/compilers).

The main difference between the two is that with a char array you have to specify the maximum size beforehand (or dynamically allocating it, but I don't think you're up to that right now as you seem to be just starting with C).

Then again, the STL's 'string' template can be a little confusing at first, especially since you're probably not familiar with templates yet.

Therefore, I suggest you start by understanding what a char array is:

For instance, if you define the following:

char mystring[30];

....defines a 30 characters array called 'mystring'.

In order to assign it a value you can do the following:

strcpy(mystring, "Hello there");

You can't just assign a string value to a char array with the equal sign - you must use the strxxx functions (check out your compiler's help system for a complete list).

Also keep in mind that C string operations operate on a NULL-terminated char array.

I hope this helps.
Avatar of adam8

ASKER

ok that will do.
if you don't specify the size is the default 1 character?
Avatar of adam8

ASKER

thanks for helping.
Not quite

  char ch;
and
  char string[1];

are very different things.

The first is a char (an integral value, usually one byte, conatining the ascii code for a character).

The second is a string that can hold 1 char (and that should be the nul, terminator anyway .. a string big enough for one character would really be char string[2];)