-2

I was trying to send the address of a string to a function in C++ I'm but facing problems like in the code below:

#include<iostream>
#include<string>
using namespace std;

void try2(string *a){
     cout<<a[2];
}

int main(){
    string a;
    getline(cin,a);//string length is greater than 10
    try2(&a);
}

I am doing the same that we do for string in C. But I don't have that much idea about C++. Please suggest and describe how string behaves in C++.

Amartya Roy
  • 21
  • 1
  • 5
  • Yeah I am going to question that this would have worked in C, either. You probably passed a c-string (char*) in C, but now you're passing a string-pointer, which would be comparable to passing (char**) in C, so you'll need to dereference it. – Zinki Dec 22 '17 at 08:14
  • 1
    `a[2]` would refer to the third element in a string *array*, but you only pass a single string object. Also you should prefer passing by (const) reference over passing a pointer – UnholySheep Dec 22 '17 at 08:15
  • 1
    off topic: i also suggest to avoid using `try` as a function name, cause it's the keyword for the `try-catch` block.. – Federico Dec 22 '17 at 08:17
  • 3
    You can't name your function `try` because its a keyword. Change the function name. Then it will compile (but do the wrong thing). – Galik Dec 22 '17 at 08:18
  • 4
    You might be interested in picking up a [good C++ book](https://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Dec 22 '17 at 08:20
  • When writing source code, you can't do trial & error. You must actually know what every single thing you write does - there's no guessing. – Lundin Dec 22 '17 at 08:58

2 Answers2

2

You can pass a string into a function as an argument and take a string reference as a parameter in the function. But you need to understand that a string isn't an array, it is an object of the string class. The reason you can use indexing on a string is because the [] operator is overloaded for the string class.

What you were doing above is passing a pointer. For it to work the way you had done it your function would have to dereference the pointer:

#include<iostream>
#include<string>

using namespace std;
void try2(string *a){
     cout<<(*a)[2];
     return;
}
int main(){
  string a;
  getline(cin,a);//string length is greater than 10
  try2(&a);
}

This would allow you to print the third character of the string object passed by reference to the function.

Note: don't use try as a function name as it is already reserved by many languages for the try-catch block syntax and can confuse the compiler.

pastaleg
  • 1,782
  • 2
  • 17
  • 23
0

You are accessing an single string as array of strings, which it is not. If you are trying to access the 2nd char of the string you have to dereference it first and then access the char like this:

#include<iostream>
#include<string>
using namespace std;
void try(string *a){
    cout<<(*a)[2];
}
int main(){
string a;
getline(cin,a);//string length is greater than 10
try(&a);
}

This is because in c++ arrays and pointer are very close to each other. *a and a[0] are almost the same which means using a[2] on a pointer isd the same as using *(a+2).

You have to keep in mind that you are not using an c-string here bit an c++-std::string which is an object and not an simple memory structure like in c.

So you first have to dereference the object before you can use it's overloaded []-operator to acess it's members (chars in this case).

Detonar
  • 1,409
  • 6
  • 18