PDA

View Full Version : cannot convert from 'char' to 'const char []'


Tlock
03-29-2006, 03:35 PM
Could some one please help me with this problem i am having. Although i have programmed in C++ for a little while now, i haven't come across this problem before. So i thought some of you pros would have seen this a few times before and would be so kind as to help me out.

functionname (char *filename, int width, int height)

which calls another function

functionname file (const char name[]);

but i can't seem to figure out how to do this conversion from

char *filename to const char name[]

error message for every attempt i have tried.

error ; cannot convert from 'char' to 'const char []'

Tlock
03-29-2006, 04:13 PM
this is the sort of stuff that makes c++ so freaking painful to relative newbies.

mummey
03-29-2006, 04:38 PM
char astring[] = "this is a string";

char * bstring;

bstring = astring;

bstring[8] = 'b';



try this, I'm pretty sure it should work.

Joscci
03-29-2006, 04:41 PM
I'm not sure I understand your question, or either I'm not able to understand your example... ? but perhaps you should declare the first argument of your first function as 'const char' also...

Maybe if you posted a little code snippet of what you're trying to do?

Cheers.

Tlock
03-29-2006, 05:01 PM
functionname (char *filename, int width, int height)
{

//mountain of code

functionname file (const char name[]);

//mountain of code
}


some warning but only one error below.

error code char *filename to const char name[]

The importance of using "char *filename" for the main call is that it needs to be called from other languages.

Tlock
03-29-2006, 05:07 PM
oh ya i have zero control over the



functionname file (const char name[]);



and



char *filename = "D:\filename.bmp"

Joscci
03-29-2006, 05:15 PM
oh ya i have zero control over the



functionname file (const char name[]);



Yeah, well it's the syntax of that particular line that's taking me off guard....

In 'THEORY' it should work, I mean... for example... the following works for me:


void bar(const char s[]) {

cout << s << endl;

}

void foo(char *s) {

bar(s);

}

void main(void) {

foo("Hello");

}


So, that is strange.

Are you taking the variable 'filename' from the first function, and passing it as an argument to the second function? I'm I assuming this right? (As I did on my example?)

Tlock
03-29-2006, 06:59 PM
After trying that again, it appears that you are correct and that there appears to be another problem. Not sure what the hell to do, i am completely stumped.

Joscci
03-29-2006, 07:41 PM
What do you mean, like a different problem?

Oh, well... that's how it is with programming... lol. You fix one problem and a brand-new one (or a whole slew of them) appears. Such is a coder's life. :shrug:

Hang in there Tlock, I'm sure you'll work things out. :)

Tlock
03-29-2006, 07:51 PM
Thanks Joscci

The other problem maybe with Visual C++ 2005. It appears that the present version of the OpenEXR code doesn't compile/execute very well. It was suggested to me to move back to VC++ 2003, but i would rather not as i simply skipped over 2002 and 2003 from VC++ 6.

tbaypaul
03-30-2006, 05:52 PM
if I understand right you are reading a single char into a char array, why not just explictly create the char array and read the single char into it as the first element.....if this is totally stupid I apoligize as I haven't worked with char much....

Tlock
03-30-2006, 10:20 PM
not stupid at all, i tried it and it should have worked. The problem is the source files provided are not fully vc++ 2005 compatible. I thought my problem was with the char but it actually wasn't.

Cronholio
03-31-2006, 04:05 AM
Is this const *char a new type defined by you or someone else's source? The solution might be as simple as changing the character set. I'm new to Visual Studio from gcc and recently ran into an error because my character set was set to Unicode and the type I was trying to insert a string into was being interpreted by the compiler as a pointer to a wide character constant. you can change the character set in the project's general properties. Setting the character set to multibyte or none at all solved the problem for me.

Also, I don't know if you are using precompiled headers, but if you try inserting the code into a new empty project it could save some trouble. I've had things I'd written on other OSs that wouldn't compile with MS's precompiled headers. Using an empty project solved my problems.

michaelcomet
03-31-2006, 07:21 PM
Not sure if this will work but have you tried explicitly casting it in the call...?

ie:

file ( (const char*)filename );
or
file ( (char*)filename );

or using void ptr perhaps

file( (void *) filename ) ;
or
file ( (const void *) filename ) ;

Not sure any of these will work tho...seems like something is odd.

scorpion007
03-31-2006, 10:08 PM
Can you please show us the exact line that is causing the error? Most likely michaelcomet's solution should work, you probably just need to cast it properly when passing it as an argument.

Tlock
04-01-2006, 03:04 PM
Even after testing explicit corrections such as the suggestions provided above (that should have worked) this is the call that fails. But not to sound like a broken record, but since i am using code compiled with VC++ 2003 and i am using VC++ 2005. Due to the design changes and greater support of the C++ Standard in VC++ 2005 older code fails. The updated code will be made available within the next couple of months. So i am not going to spend time fixing their old.

My only other option is to use their C version dll, which i am considering. Let's see how impatient i get, but i do appreciate all the work they do.

NOT MATTER WHAT YOU DO THIS EXACT CALL FAILS - BELIEVE ME I HAVE TRIED IT ALL...

RgbaInputFile file (filename);

rendermaniac
04-01-2006, 05:45 PM
If you haven't done so already it might be worth signing up to the OpenEXR mailing list and asking them - the ILM developers are pretty responsive about this sort of thing.

Simon

Tlock
04-01-2006, 05:49 PM
They are the ones who told me about the VC++ 2003 - 2005 issue, and yes they are amazingly supportive.

scorpion007
04-06-2006, 07:11 AM
NOT MATTER WHAT YOU DO THIS EXACT CALL FAILS - BELIEVE ME I HAVE TRIED IT ALL...

RgbaInputFile file (filename);

Well of course RgbaInputFile file (filename); will fail, you are passing a char* where it asks for a const char*.
Did you try RgbaInputFile file ((const char*) filename);

tbaypaul
04-06-2006, 04:53 PM
I guess I just find it hard to believe that VC++2005 will not let you retrieve the string filename from the char pointer and explictly feed it into a constant char array?? An ugly hack perhaps! It just seems so fundamental an issue, that testing would have certainly found it before the compiler was released.

scorpion007
04-09-2006, 08:25 AM
BTW, just to clarify, a char[] is equivalernt to a char* . Just as the following lines are equivalent:

int main(int argc, char** argv)
int main(int argc, char* argv[])


An array is basically a pointer to the address of the first element.
So if you have

int myArr[3] = {2, 6, 4};
int* pInt = myArr;
printf("*pInt == %d\n", *pInt); // --> 2
printf("myArr[0] == %d\n", myArr[0]); // --> 2 (same thing)

CGTalk Moderation
04-09-2006, 08:25 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.