Compiler Error C3834
Visual Studio 2015
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at Compiler Error C3834.
illegal explicit cast to a pinning pointer; use a pinned local variable instead
Explicit casts to a pinned pointer are not allowed.
The following sample generates C3834.
// C3834.cpp
// compile with: /clr
int main() {
int x = 33;
pin_ptr<int> p = safe_cast<pin_ptr<int> >(&x); // C3834
pin_ptr<int> p2 = &x; // OK
}
The following sample generates C3834.
// C3834_b.cpp
// compile with: /clr:oldSyntax
using namespace System;
int main() {
const __wchar_t * pS = reinterpret_cast<const __wchar_t __pin*>(S"hello"); // C3834
// OK
// declare a pinning variable
String __pin* ppin = S"Hello";
// cast pinning variable to unmanaged type
const __wchar_t * pS2 = reinterpret_cast<__wchar_t*>(ppin);
pS2 += 6;
}
Show: