Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


C++ String to array

pistolizedpetepistolizedpete Member Posts: 4
Hi there,

New to C++ and trying to write a short program to reverse the characters in a word. Im having trouble converting the string to a char array. Here is the code:


#include "stdafx.h"

using namespace std;

int main()
string org;
string end;
cin >> org;

int num = org.size();

char reversal[num];
strcpy(reversal, org.c_str());

for (int a = num; a >= 0; a--)
cout << reversal[a];
cout << endl;
return 0;


Im getting three errors. All are on the strcopy() line:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'reversal' : unknown size

Im using Visual C++ to compile.

Any advice you can give is much appreciated and if there is a more efficient way of doing this please let me know. Thanks again!



Sign In or Register to comment.