Conversion between char [] array, char * pointer, STD: string *

Source: Internet
Author: User

In the small game of cocos2d-x, the char * type string needs to be forcibly converted to void * pointer type, passed to the callback function through cccallfuncnd.

It is possible to find this:

// (A) is created using a string in double quotation marks.

Enemy * enemy = enemy: Create (this, "enemy/enemy2r.png ");

...

// (B) initialize

_ Bornaction = ccsequence: Create (ccanimate: Create (blastanimation ),
Cccallfuncnd: Create (this, callfuncnd_selector (tank: borncallback), (void *) filename ),
Null );

...

// (C) callback function

Char * STR = (char *) data;

The above method is acceptable. However, if you replace (a) with the following string:

Char STR [64] = {0 };
Sprintf (STR, "enemy/enemy1_dr.png", random );
Enemy * enemy = enemy: Create (this, STR );

In this case, the data obtained in the callback function is empty (for unknown reasons, do not spray the data ).

You can convert char * to STD: string * and then to void *. The data obtained by the callback function is normal. As follows:

_ Bornaction = ccsequence: Create (ccanimate: Create (blastanimation ),
Cccallfuncnd: Create (this, callfuncnd_selector (tank: borncallback), (void *) (new STD: string (filename ))),
Null );

...

// Callback

STD: string * STR = (STD: string *) data)-> c_str ();

The converted data will be normal. I do not know the specific reason. I guess there may be some differences between char [], char *, and "ABC.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.