LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-07-2011, 09:43 PM   #1
Zssfssz
Member
 
Registered: Sep 2011
Location: Las Vegas!
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339

Rep: Reputation: Disabled
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.
 
Old 11-07-2011, 10:18 PM   #2
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
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.

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

Last edited by MrCode; 11-07-2011 at 10:32 PM. Reason: added code example
 
Old 11-08-2011, 03:22 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.

Last edited by Nylex; 11-08-2011 at 03:25 AM.
 
Old 11-08-2011, 08:22 AM   #4
Zssfssz
Member
 
Registered: Sep 2011
Location: Las Vegas!
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339

Original Poster
Rep: Reputation: Disabled
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.

Last edited by Zssfssz; 11-08-2011 at 08:26 AM.
 
Old 01-21-2012, 12:30 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid conversion from `int*' to `socklen_t*' r350 Programming 5 10-02-2011 05:34 PM
invalid conversion m.dehghan Programming 1 07-28-2011 05:18 AM
Invalid conversion from bool fsshl Programming 3 07-19-2011 06:42 PM
Invalid conversion from ‘char’ to ‘packet_t’ mcpoon Programming 4 05-25-2010 03:55 AM
invalid conversion from `char' to `const ch predatorz Programming 3 11-15-2008 01:00 PM


All times are GMT -5. The time now is 07:06 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration