DelphiFAQ Home Search:

Copying a string into an array of char in managed code

 

comments4 comments. Current rating: 3 stars (1 votes). Leave comments and/ or rate it.

Question:

I've got an array of char in a managed class as declared below and need to initialize it with strcpy(). (I'm porting old code.) I get the following error message:
error C2664: 'strcpy' : cannot convert parameter 1 from 'char [255]' to 'char *'

Answer:

The array data is physically part of a __gc object, and thus the corresponding pointer type is an interior __gc pointer. To pass it to strcpy, you'll have to convert it to a pinning pointer using the __pin keyword - as shown in the code below.

You can reuse that pinning pointer for other arrays of char without casing a memory leak. In addition, you can unpin an object by assigning NULL to the pinning pointer.

public __gc class CMyClass{ 
public:
   data char[255];
}


// ..

CMyClass::CMyClass() {

  // this will not work
  strcpy(data, "Start");

  // the following will work
  char __pin * p;
  p = data; strcpy(p, "Start");

  // ..
}

Comments:

2012-06-05, 21:29:43
anonymous from China  
<strong> http://www.coachout..strong> <a href=' http://www.coachout..g>Coach Outlet Online</strong></a><br />
<strong> http://www.coachfac..strong> <a href=' http://www.coachfac..g>Coach Factory</strong></a><br />
<strong> http://www.myloveco..strong> <a href=' http://www.myloveco..g>Coach Outlet Online</strong></a><br />
<strong> http://www.buyincoa..strong> <a href=' http://www.buyincoa..g>Coach Outlet</strong></a><br />
<strong> http://www.cheapcoa..strong> <a href=' http://www.cheapcoa..g>Cheap Coach Handbags</strong></a><br />
<strong> http://www.coachfac..strong> <a href=' http://www.coachfac..g>Coach Factory Outlet</strong></a><br />
<strong> http://www.buycoach..strong> <a href=' http://www.buycoach..g>Coach Outlet Online</strong></a><br />
<strong>www.coachoutletonfactory.net</strong> <a href=' http://www.coachout..g>Coach Factory Outlet</strong></a><br />
<strong>www.coachoutletonline4you.us</strong> <a href=' http://www.coachout..g>Coach Outlet Online</strong></a><br />
<strong>coachfactoryoutletq.net</strong> <a href=' http://www.coachfac..g>Coach Factory</strong></a><br />
<strong>gotocoachoutlets.net</strong> <a href=' http://www.gotocoac..g>Coach Outlet Online</strong></a><br />
2012-11-12, 01:59:21
coach001 from United Kingdom  
2012-11-12, 04:03:30
anonymous from United States  
2013-02-09, 20:40:31
 
rating
otpuwukl <a href=' http://viagraonlinete.com/ '>viagra online</a> 1072 <a href=' http://cialisonlinete.com/ '>cialis</a> 0623

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: