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
LVL 1
adam8Asked:
Who is Participating?
 
bbousquetConnect With a Mentor Commented:
You're right, RONSLOW...

I should have submitted my comment as an answer.

Here. It's corrected now.
0
 
bbousquetCommented:
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


0
 
RONSLOWCommented:
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).
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
ntdragonCommented:
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

0
 
anemosCommented:
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.
0
 
adam8Author Commented:
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
0
 
bbousquetCommented:
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.
0
 
adam8Author Commented:
ok that will do.
if you don't specify the size is the default 1 character?
0
 
adam8Author Commented:
thanks for helping.
0
 
RONSLOWCommented:
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];)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.