C++ and Strings!

Started by
6 comments, last by naimwslim04 19 years, 3 months ago
I'm new to C++ programming, and I really don't understand how to work with strings. It is extremely confusing. I can do simple declarations of strings, like for example: char name[80] which makes a string that can hold 80 characters. However, I get stuck on things like functions returning data types. I understand that int functions return integers, bool functions return true/false, void functions do not return a value, etc. But let's say you want a function to return a string, how would you do that? Everytime I try to apply the knowledge that int functions return integers, etc, I keep getting errors when I use this with char. The most common ones are things like "CANNOT CONVERT CONST CHAR * TO CHAR[80]" or something along the lines. I know that this is probably something that is second nature to you all, but as I'm rather new to C++ programming, having just moved from BASIC (where there is a data type called STRING!) this is something rather confusing. I understand the logic behind the different manner in C++, but it's rather confusing for me! Thank you, Naim
Advertisement
Hi there,

take a look at STL (the standard template libary) of c++
they have a class named string which makes working with them really easy.

working with char arrays "char foo[...]" can be rather confusing in the beginning.
Basically you cant just do:
char a[40];a = function(...);


because the semantics for a string is not built into c++.
In C++ avoid using char arrays for strings, instead use std::string which makes everything a whole lot easier as it takes care of things like memory management for you. The std::string class also contains various functions for searching through strings joining two strings together etc.
Firstly, in C++ it is usually better to use the string class. An example of using the string class would be.

#include<ostream>#include<string>#include<iostream>int main(){std::string mystring = "Hello world";std::cout << mystring;}


That is the hello world of the string class. That aside, to return a character array string from a function is not as easy as other types. The reason for this is that pass by value semantics are not as straight forward for arrays as the are for other types such as int. You cannot copy arrays in the same way, it requires additional function calls. For example

int main(){char string[] = "hello";char otherstring[] = string; //will not compile}


This prevents arrays from being returned from functions by value. The only option open to you is to return a pointer to the string. There is also a problem here in that if you allocate the string on the stack inside the function, the memory will be released once the function exits, and so the pointer will be pointing to invalid memory, as demonstrated here.

char* function(){char string[] = "hello";return string;}int main(){char* pstring = function(); //pstring now points to invalid memory}


There are a few ways around this, namely not allocating the array on the stack, but these solutions tend to be more trouble than they are worth.

This is where the string class comes in (as mentioned earlier). the string class can be returned by value, it can be copied (unlike arrays). This means that instead of trying to return an array of characters, you return a string.

#include<string>#include<ostream>#include<iostream>std::string function(){std::string mystring = "hello";return mystring;}int main(){std::string astring;astring = function();std::cout << astring;}


The string class also has a lot of other cool features, such as appending strings together using string = string + string, these features are easily researched in any good C++ standard library book.
try this
#include <iostream>#include <string> //header that lets you declare stringsusing namespace std;int main() {  string name; //string declaration  name = "Adam"; //wow we can use strings now!  cout<<name<<endl;  return 0;}

Hope this helps =)
Meta AdamOne of a million noob C++ programmers.
There are two basic ways that you can reference string data.

1. You can create a compile-time constant size array on the stack:
char myString[80];

2. You can refer to any array of characters (on the stack, or statically or dynamically created on the heap) by using a char pointer:

char myString[80];
char* stringPtr = myString; //This points the pointer at memory on the stack which will be cleaned up when the myString variable goes out of scope
stringPtr = new char[80]; //This points the pointer at newly allocated memory, which must manually be cleaned up when it is no longer needed by using delete

If you want to return a string to a calling method, one way is to use a char pointer that points to a buffer on the heap that has been filled with string data. You can also return string literals this way (without having to worry about deallocating memory):

char* myStringFunc(bool input) {

if(input) return "The value is true";
else return "The value is false";

}

The above function doesn't dynamically allocate memory on the heap, so the returned result doesn't need to be cleaned up. The following function illustrates the use of dynamically created memory:

char* myStringFunc(bool input) {

char* output = new char[30]; //Dynamically allocate some memory

if(input) strcpy(output, "The value is true");
else strcpy(output, "The value is false");

return output;

}

Since the string in the above function is dynamically allocated, it is the caller's responsibility to see that the char array is deleted when it is no longer needed.

Many functions, instead of returning a pointer, will accept a char pointer argument that points to a previously allocated buffer, which the function fills with string data. This way, the caller gets to decide whether to allocate memory on the heap or on the stack.

If all of this seems a little too complicated or even if it doesn't, you should look into the string class, which can simplify everything I went over above.
Wow! There weren't any replies when I started mine. Good forum! [lol]
Quote:Original post by TheBluMage
Wow! There weren't any replies when I started mine. Good forum! [lol]

Too true, I'd say! Thanks for all your help!

This topic is closed to new replies.

Advertisement