DelphiFAQ Home Search:
General :: Programming :: C++


This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

Copying a string into an array of char in managed code


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 *'


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{ 
    data char[255];
 // ..
 CMyClass::CMyClass() {
   // this will not work
   strcpy(data, "Start");
   // the following will work
   char __pin * p;
   p = data; strcpy(p, "Start");
   // ..

Generated 0:01:38 on Nov 25, 2017