LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Invalid Conversion from... (https://www.linuxquestions.org/questions/programming-9/invalid-conversion-from-912400/)

Zssfssz 11-07-2011 09:43 PM

Invalid Conversion from...
 
Ok I'm gettin the "Invalid convertion from 'char*' to 'char'" thing. This is different from yer usual error because I'm actually working with pointers.
Here is a boiled down verstuon:
#includes
char* LOC = new char[501]; //This line had no errors
char ret[501]="This is 501 charecters in my program";
*LOC = ret; //This is where the error is
End code
Well anyone?
The heap data is deleted at the end of the program.

MrCode 11-07-2011 10:18 PM

It would be better if you posted your code in [code][/code] tags from now on; it puts code in a box with a monospaced font:

Code:

#include <stdio.h>

int main(int argc,char** argv)
{
    printf("Like this.\n");
    return 0;
}

As to your problem, my C is a little rusty, but this is how I see it:

it looks to me like you're trying to assign the pointer to the first element of ret as the first element of LOC, not the elements themselves.

i.e.:

char* LOC = new char[501]; dynamically allocates 501 bytes of memory and assigns it to the pointer LOC.

char ret[501]="This is 501 charecters in my program"; allocates 501 bytes of memory on the heap and fills them with the values of the string "This is 501 charecters in my program".

*LOC = ret; is attempting to assign the pointer to the ret array as the dereferenced value of (i.e. the first element of) the LOC array, so you're implicitly converting from char* to char.

I think what you want to do is strcpy the contents of ret into where LOC points to. Something like:

Code:

char* LOC = new char[501];
char ret[501]="This is 501 charecters in my program";

strcpy(LOC,ret);

I sincerely apologize to all if I'm way off on this…I'm just trying to help. :scratch:

(…awaits abrasive correction from other, more knowledgable members…)

Nylex 11-08-2011 03:22 AM

Are you using C or C++? I didn't think new was a keyword in C. If you're using C++, is there any reason you're not using C++ strings?

Edit: Also, is there any reason you don't have spell checking enabled in your browser? A lot of your posts contain typos.

Zssfssz 11-08-2011 08:22 AM

I thought there wernt any poi ters in c. Well Using C++. I do have spell check and that's the problem; when you see me posting from a Mac I'm really posting from my iPod Touch (see! See! It will automatically capitalize any of it's products).
I need to use c-style strigns to work with filenames, using a normal string gets me an error.
Thanks peeps ya got the anwnswer!
Edit: I had no idea that the code tags existed in this forum.

Nylex 01-21-2012 12:30 AM

If you need a C string, you can always use C++ strings to do any manipulation (e.g. concatenation) and then call the c_str() method. See this for a list of string's member functions.


All times are GMT -5. The time now is 10:21 AM.