C-strings and strings

Hi.
I'm having trouble understanding how to pass strings and char arrays to functions. There are all sorts of conversion errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

using namespace std;

void reverse(string& str);

  int main()
{
	cout << "Enter a string: ";
	string s;
	getline(cin, s);

	reverse(s);


	cin.ignore();
	cin.get();
	return 0;
}

void reverse(string& str)
{
	const char* cstr = str.c_str();
	for (int i = strlen(cstr); i > 0; --i)
	{
		strcpy(str, cstr[i]);
	}
}


This was meant to reverse strings.

But my question pertains to strings and c-strings in general. If someone can explain to me in-depth and with examples, their relationships and how they work when passed to functions I would appreciate it. I've read the entire chapter in the course I'm learning from and I've already searched online, but no one has given a full explanation, just patchwork codes.

Thanks.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

string reverse (string s1) {
	int i=0, len = s1.length();
	for(;i<len-1;i++,len--)
		swap (s1[i],s1[len-1]);
	return s1;
}

int main () {
	string s = "abc de";	
	cout << reverse(s);
	
 return 0;
}
my question pertains to strings and c-strings in general. If someone can explain to me in-depth

A C-string is simply an array of chars terminated by a null character.
When passed to a function, it is always passed to the function as a pointer.
1
2
3
4
5
6
 
  char cstr[10];  // Holds up to 9 chars + terminating null
  void somefunc (char * str);   // Function declaration

  somefunc (cstr);  // Pass the array to somefunc
  somefunc (&cstr[2]);  //  Pass array starting with 3rd char  


std::string on the other hand is a C++ class. The std::string class maintains internal storage for the contents of the string. std::string provides semantics to retrieve and assign specific characters, concatenate another string, etc. std:string can be passed to a function by value (a copy is made), by reference, or via a pointer.
1
2
3
4
5
6
7
8
  std::string  str ("some text");
  void func1  (std::string str);  // Accepts str by value (a copy)
  void func2 (std::string & str); // accepts str by reference 
  void func3 (std::string * str);  // Accepts a pointer to a string object

  func1 (str);  //  pass by value
  func2 (str);  // pass by reference
  func3 (&str); // pass a pointer to the string object.  


Note that taking the address of a string returns the address of the string object, not the address of the internal representation of the string. If you want to pass a std:;string to a C function, then you use the c_str() method.

Does that help?



It does help a lot, thanks :D

But what if I want to pass a string to a function that will then treat it as a char array, or vice-versa?

Something like:

1
2
3
4
5
6
7
void func(char*str);

int main()
{
    string str = "abcd";
    func(str);
}


How would you make this work? I have encountered exercises where I was supposed to pass strings into a function that will do c-string operations on it. Can this work directly or do I have to convert the string into a char array inside the function and then work on it?
As I mentioned previously, std::string has the c_str() function that returns a const pointer to the internal representation of the string.

1
2
3
4
5
6
void func (const char*str);

int main()
{   string str = "abcd";
    func (str.c_str());
}


Note that std::string has no function for returning a non-const pointer to the internal representation. You can however, do something like
1
2
    char * ptr; 
    ptr = &str[0];  // Pointer to first character 

although this is legal, it's considered poor style since it perverts the encapsulation provided by std::string.

Last edited on
Thanks for all your explanations. I'll be sure to bookmark this for future reference.
Topic archived. No new replies allowed.