LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ help Dynamic array and "invalid conversion from ‘char’ to ‘char*’" (https://www.linuxquestions.org/questions/programming-9/c-help-dynamic-array-and-invalid-conversion-from-%91char%92-to-%91char%2A%92-721696/)

heathf 04-25-2009 09:13 PM

C++ help Dynamic array and "invalid conversion from ‘char’ to ‘char*’"
 
i have most of my code finished but im getting an error i dont understand "invalid conversion from ‘char’ to ‘char*’" and i need to write to a dynamic array and then output from it which i also dont know how to do. any help would be greatly appreciated. thanks for your time.

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

void rearrange (char *front, char *rear);

int main()
{
string input, word;
char *front, *rear;
vector <string> string;
cout << "Enter the string: ";
getline(cin, input);

istringstream break_apart(input);

while (break_apart >> word)
{
front = &word.at(0);
rear = &word.at(word.size() - 1);
while (front <= rear)
{
rearrange (*front, *rear); // <--- heres where im getting my error
front++;
rear--;
}
cout << word << " ";
}



return 0;
}

void rearrange (char *front, char *rear)
{
char temp = *front;
*front = *rear;
*rear = temp;
}

heathf 04-25-2009 09:15 PM

well i was playing with it and i fixed my error but i still dont know what to for the dynamic array

ilikemonkeys111 04-25-2009 09:20 PM

The problem is because your rearrange function takes two char pointers: *front and *rear. However, you are dereferencing front and rear when you call the function, therefore you're passing two chars by value when the function expects two pointers. To fix it, you're going to want to change the function call to

Code:

rearrange (front, rear);
That should fix your problem :-{D


All times are GMT -5. The time now is 11:25 AM.