Question

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;
}   
Was it helpful?

Solution

There are many problems here.

If you really want to return a single character that should be

return char32[0];

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 char32 is a global it's not really adding any value above the function returning void - the caller can just access char32 directly. If you want to output the results using cout it would be better to return a new string from parse.

You should stop the loop in parse when you reach min(size, 32), or you will overrun the input string in line with 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:

const size_t MAX_LENGTH(32);

If all parse does is byte-by-byte copy, you could use std::copy but 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 to char myresult = parse(line); before you output char32 to cout by the way.

OTHER TIPS

The type of char32 is char (*)[32] (which degenerates to char *), that is, an array of 32 chars. This is quite different from char which is a single char. If you want to return the whole array, change your return type. otherwise, return just one character: char[x] where x is the desired index.

the compiler is telling you the error. you said that parse returns one char.

char parse(....

char32 is an array of chars; an array of chars is of type char*.

So when you go

return char32;

you are contradicting what you said earlier

What should parse return - one char or an array of chars? Pick one and be consistent

Your function returns type char, but Char32 is of type char *. From what it looks like you're trying to do, you'd want to change your function's return type.

There are a number of problems I can see with this. Some of which have already been mentioned by others.

1) The return type of parse() is a char, but you're returning a char*

2) Your parse() function is redundant over either the myString.c_str(); which represents the string as a char array, or the myString.copy(char32, 32); method which copies the first 32 chars of the string into the array. (be aware that the last char won't be the null terminating character '\0' indicating the end of the string)

3) Your code never actually places the input's data into char32. You're forgetting a line in main like this: char* char32 = parse(line);

All-in-all, your code would be improved by doing the following:

int main()
{
    string line;
    ifstream vgv;
    vgv.open("some file");
    if(!vgv)
        return 1; // failure

    getline(vgv, line);
    char* char32 = new char[32];
    line.copy(char32,31);
    char32[31] = '\0'; //Ensure that the string is actually terminated
    cout << char32;

    delete [] char32;
    return 0; // success
}

The main problem with what I've presented is that, if line's data is less than 31 characters, you'll see junk data when you print char32. So it's better to my replace char32[31] = '\0' with the following:

int size = line.size();
char32[(size > 31) ? 31 : size] ='\0';

Lastly, if you ever want to find the size of the data within char32, you can do this:

int size = 0;
while(char32[size++] != '\0' && size < 32){} // gets size of the valid data within char32

-- UPDATE --

If you really want to use a parsing function, here is my best recommendation.

char* parse(string line, int quantity)
{
    char* rtn = new char[quantity];
    line.copy(rtn, quantity-1);

    int size = line.size();
    rtn[(size > quantity-1) ? quantity-1 : size] = '\0';

    return rtn;
}

int main()
{
    string line = "asdfsadfsdaf";

    char* myDataOne = parse(line, 32);
    char* myDataTwo = parse(line, 5);

    cout << myDataOne << endl;
    cout << myDataTwo << endl;

    delete [] myDataOne;
    delete [] myDataTwo;
    return 0; // success
}

While unclear to me what are you trying to achieve with this code, I'll tackle the problem syntactically.

char parse(), as declared, returns a char, that is, a single character. If you want it to return an array of characters, you should change the return value to char*. That way you can simply do return char32;. Otherwise, you must pick a character from the array char32 by indexing it. *char32 would then be equivalent to char32[0].

As a final note, in C++, you'd better stick to using std::strings rather than plain char arrays or pointers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top