Reputation: 3465
I have a function that takes a c++ style string. I want to either put argv[1]
into a c++ string, or simply pass it directly to my function.
#include <iostream>
#include <string>
using namespace std;
string rev (string &reverse);
int main(int argc, char argv[])
{
if (argc != 2)
cout << "Bad Input" << endl;
string reverse = argv[1];
cout << rev(reverse) << endl;
}
This is what I have so far, but when I do this, I get the following error:
8.4.cpp: In function ‘int main(int, char*)’:
8.4.cpp:11: error: invalid conversion from ‘char’ to ‘const char*’
8.4.cpp:11: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’
Upvotes: 0
Views: 6409
Reputation: 44
As mentioned argv[]
is a pointer, to a block of memory, or a "pointer to a pointer".
You simply need to add *
to argv[]
to make it act such.
A quick Google brought up THIS
Upvotes: 0
Reputation: 264421
Fix main
int main(int argc, char argv[])
// should be
int main(int argc, char * argv[])
/// ^^^^^
Upvotes: 6