views:

177

answers:

6

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 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.

Any and all help with this is appreciated.


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;
}   
+2  A: 

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.

Steve Townsend
Thanks a lot for the help, and for the advice here and in the comment. This has helped me far more than just with this question
Chris Wilson
@Chris - You are quite welcome. C++ is quite a paradigm shift.
Steve Townsend
+4  A: 

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.

JoshD
+2  A: 

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.

Tristan
To split hairs, char32 isn't a char *, it's a char array. There's a subtle difference as explained here: http://stackoverflow.com/questions/2397792/difference-between-pointer-to-int-and-pointer-to-array-of-ints
JoshD
fair enough, though this was based on the error given, and Char32 is indeed the address of the first element, which the compiler treats as a char*
Tristan
+3  A: 

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

pm100
+1  A: 

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.

Alek
+2  A: 

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
}
Legatou
In both C and C++, an array is NOT simply a pointer to the first element. In C, you really couldn't tell except for `sizeof` but with C++ there are lots of places where the two act differently.
Ben Voigt
Fair enough, but this isn't reason enough to down-vote. I gave him loads of useful information. I'll remove my technically incorrect statement, though.
Legatou