My code, when compiled, returns the error invalid conversion from ‘char*’ to ‘char’, and identifies the second last line, return char32 as the problem. I’ve searched the Internet and this site and haven’t found anything that has helped. I’ve found the error goes away and the code compiles if I replace the problem line with return *char32, but I’m not entirely sure why this should be the case. I understand that this is pointer notation, but I’m unsure how they apply to this situation. Also, when I do this and attempt to print *char32 to the terminal, nothing happens, indicating that *char32 in empty. I’ve also verified that there is indeed a line being read in from the file some file.
Update 1: I am attempting to return a full array from parse().
Update 2: I realise I didn’t state my goal for this code. I am attempting to read the first line from the some file, pass it to parse() and have the first 32 characters indexed into the array char32, which will then be passed back to main(). Sorry for the confusion.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
char parse(string line), char32[32];
int main()
{
string line;
ifstream vgv;
vgv.open("some file");
getline(vgv, line);
cout << char32;
return 0;
}
char parse(string line)
{
int size = line.size();
for (int count1 = 0; count1 < 32; count1 ++)
{
char32[count1] = line[count1];
}
return char32;
}
There are many problems here.
If you really want to return a single character that should be
or whichever character you want, by index. This is the same as *char32 by the way. An array variable can be treated as a pointer to the first array in the element, if no index is used. Although since
char32is a global it’s not really adding any value above the function returning void – the caller can just accesschar32directly. If you want to output the results usingcoutit would be better to return a newstringfromparse.You should stop the loop in
parsewhen you reach min(size, 32), or you will overrun the input string inlinewith undefined results (bad, typically). it would be a good idea to allow space for a null terminator here – again using string would just obviate this.Magic numbers are evil. Use this instead of the hard-coded 32:
If all parse does is byte-by-byte copy, you could use
std::copybut I suspect this is a prototype for a real parser you plan to build once interface issues are resolved. I think you are missing a call tochar myresult = parse(line);before you outputchar32tocoutby the way.