Solved

easy question

Posted on 2000-04-03
10
159 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:adam8
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:bbousquet
Comment Utility
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
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
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
 

Expert Comment

by:anemos
Comment Utility
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
 
LVL 2

Accepted Solution

by:
bbousquet earned 20 total points
Comment Utility
You're right, RONSLOW...

I should have submitted my comment as an answer.

Here. It's corrected now.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:adam8
Comment Utility
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
 
LVL 2

Expert Comment

by:bbousquet
Comment Utility
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
 
LVL 1

Author Comment

by:adam8
Comment Utility
ok that will do.
if you don't specify the size is the default 1 character?
0
 
LVL 1

Author Comment

by:adam8
Comment Utility
thanks for helping.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now