Solved

easy question

Posted on 2000-04-03
10
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:bbousquet
ID: 2682669
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
ID: 2682743
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
ID: 2683063
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:anemos
ID: 2684510
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
ID: 2684629
You're right, RONSLOW...

I should have submitted my comment as an answer.

Here. It's corrected now.
0
 
LVL 1

Author Comment

by:adam8
ID: 2685528
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
ID: 2685716
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
ID: 2686379
ok that will do.
if you don't specify the size is the default 1 character?
0
 
LVL 1

Author Comment

by:adam8
ID: 2686382
thanks for helping.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2686391
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

749 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