Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Softimage® / XSI SDK / CString (const char *) bug
  RSS 2.0 ATOM  
2 pages: 1.2 last

CString (const char *) bug
Rate this thread
 
30200
 
Permlink of this thread  
avatar
  • Total Posts: 8
  • Joined: 09 March 2008 12:58 AM

CString(const char * instr) is bugged. Try

char name[] = “xsi”;
CString myStr(&name);

Application().LogMessage(myStr);

You’ll get a print out like 030328 etc, probably an address. A workaround is use PutAsciiString(&name). That works.



Replies: 0
avatar

Actually, it works perfectly fine.
Your &name refer to an address in the memory which is an integer value, therefore its the constructor CString::CString(const CValue& in_val) that is called since a CValue object can be implicitly constructed using an integer value.

Try using CString myStr((char*)&name) instead, should work.

Edit: woops, just noticed that I didn’t removed the & in the hurry, sorry for the misconfusion.



Replies: 0
avatar

Thanks for pointing that out. I’ll try it when I get home. But implicit type conversion is bad practice in a static type language.



Replies: 0
avatar

Even if that implicit conversion was not there, I don’t think that passing an integer value and hoping that it will be hopefully interpreted as a char pointer is a good practice as well.

Cheers,
Christopher.



Replies: 0
avatar

I tried CString myStr((char*)&name), and it didn’t work. It still prints out an address. I am not sure what it’s doing internally. It should just work as

char name[] = “stuff”;
CString myStr(name);

Since char arrays are decayed into pointers. I made a mistake to use &name, it doesn’t really make sense in this case. Pointers are typed, char * is not int *, even though they could contain the same value. There shouldn’t be any type confusions or which constructor to call in this case.



Replies: 0
avatar

Hey, you got me! I just looked at the doc and there is no CString(const char*) constructor...! :)
You probably confused with the one using wchar_t* as input.



Replies: 0
avatar

CString(const char*) is new in 7.5. I’m going to send an email to softimage support.



Replies: 0
avatar

Oh, sorry then. I’m still using the 7.0 version.



Replies: 0
avatar
  • BenR
  • Posted: 16 May 2009 05:32 AM

When I test it out it works fine passing name to the constructor. As you implied above, your initial code passing &name was passing a char**.



Replies: 0
avatar
  • luceric
  • Posted: 16 May 2009 06:07 AM

your code is wrong.  “name” is already a pointer, why do you take the address of it?

The correct way to use this is

char name[] = “xsi”;
CString myStr(name);

[quote=louisfeng;22817]A workaround is use PutAsciiString(&name). That works. also wrong, there should be no & there

you may be confused by the C language. [B]name [/B]is of type [B]char *[/B]
[B]char name[] = “xsi”;[/B]
is equivalent to
[B]char *name = “xsi”;[/B]
(there is a technical storage difference, but it “name” is the same type in both case)



Replies: 0
avatar

[quote=luceric;22885]your code is wrong.  “name” is already a pointer, why do you take the address of it?

The correct way to use this is

char name[] = “xsi”;
CString myStr(name);

also wrong, there should be no & there

you may be confused by the C language. [B]name [/B]is of type [B]char *[/B]
[B]char name[] = “xsi”;[/B]
is equivalent to
[B]char *name = “xsi”;[/B]
(there is a technical storage difference, but it “name” is the same type in both case)

Uhh, did you actually try to run this code? Did it actually print out “xsi”? Try this with 7.5 SDK:

Application app Application();
  
char hi[] "xsi";
  
CString msg(hi);
  
app.LogMessage(msg);

And tell me what it prints out.

char [] (array) is certainly not the same type as char *. They are “equivalent” in the sense that functions accepting char * will take char [] because char arrays will *decay* into pointers when passed as parameters. But you certainly can’t assign like below.

char array[] "v1";
char pointer "v2";
pointer = array; // ok, automatically converted
array = pointer// error, can't convert array type to pointer type

So in the first example I gave in the my first post what does &name give you? It’s a pointer to an array. I tried it because passing in the char [] array like CString str(name) did *not* work properly, printing out a number rather than the actual string. So I tried all kinds of variations to pass in this char*, none of them worked properly.

The SDK is bundled with XSI version 7.5.2009.0204.



Replies: 0
2 pages: 1.2 last