How to use strcpy to modify char** ?
-
The
strcpy
below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.char **pArrayLabel = NULL; // I have to declare it this way, no vector.
// (It will be pass to another function later)int size=12;
pArrayLabel = new char*[size];CString szLabel0 = "Januari";
strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength())); -
The
strcpy
below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.char **pArrayLabel = NULL; // I have to declare it this way, no vector.
// (It will be pass to another function later)int size=12;
pArrayLabel = new char*[size];CString szLabel0 = "Januari";
strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));You're copying into an uninitialized pointer. You need to allocate a
char
buffer for each of the 12char*
s--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.
-
The
strcpy
below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.char **pArrayLabel = NULL; // I have to declare it this way, no vector.
// (It will be pass to another function later)int size=12;
pArrayLabel = new char*[size];CString szLabel0 = "Januari";
strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));I would do this: char *pArrayLabel = NULL; int size=12; pArrayLabel = new char[size]; CString szLabel0 = "Januari"; strcpy(pArrayLabel,(char *)szLabel0.GetBuffer(szLabel0.GetLength())); And when you pass it to the other function, pass it as "&pArrayLabel"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc.](http://www.soonr.com)
-
The
strcpy
below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.char **pArrayLabel = NULL; // I have to declare it this way, no vector.
// (It will be pass to another function later)int size=12;
pArrayLabel = new char*[size];CString szLabel0 = "Januari";
strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));once again, somebody is using
CString::GetBuffer()
when it's definitely not needed. Call GetBuffer() only if you need to modify the CString object directly by writing inside its internal buffer. and this call to GetBuffer() requires a call to ReleaseBuffer() (which you don't do here). if you only need to get the string contained within the CString, then you must use the cast operator(LPCTSTR)
and that's it. and for your problem, as that's been stated, you're assigning a NULL pointer. modify your code into this. ans at last, as i'm on it, please be careful with your variables names (generally, a variable starting with sz or psz is declared being a C-Style string)const int iSize = 12;
TCHAR *pArrayLabel = ::new TCHAR[iSize];
CString strLabel = _T("Januari");
::_tcscpy_s(pArrayLabel[0], (LPCTSTR)szLabel0);
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
once again, somebody is using
CString::GetBuffer()
when it's definitely not needed. Call GetBuffer() only if you need to modify the CString object directly by writing inside its internal buffer. and this call to GetBuffer() requires a call to ReleaseBuffer() (which you don't do here). if you only need to get the string contained within the CString, then you must use the cast operator(LPCTSTR)
and that's it. and for your problem, as that's been stated, you're assigning a NULL pointer. modify your code into this. ans at last, as i'm on it, please be careful with your variables names (generally, a variable starting with sz or psz is declared being a C-Style string)const int iSize = 12;
TCHAR *pArrayLabel = ::new TCHAR[iSize];
CString strLabel = _T("Januari");
::_tcscpy_s(pArrayLabel[0], (LPCTSTR)szLabel0);
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
once again, somebody is using CString::GetBuffer() when it's definitely not needed.
:laugh: Yeah, they should charge a toll $$$ to use CString::GetBuffer() and CWnd::PreTranslateMessage() to help people consider alternative solutions :)
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
once again, somebody is using
CString::GetBuffer()
when it's definitely not needed. Call GetBuffer() only if you need to modify the CString object directly by writing inside its internal buffer. and this call to GetBuffer() requires a call to ReleaseBuffer() (which you don't do here). if you only need to get the string contained within the CString, then you must use the cast operator(LPCTSTR)
and that's it. and for your problem, as that's been stated, you're assigning a NULL pointer. modify your code into this. ans at last, as i'm on it, please be careful with your variables names (generally, a variable starting with sz or psz is declared being a C-Style string)const int iSize = 12;
TCHAR *pArrayLabel = ::new TCHAR[iSize];
CString strLabel = _T("Januari");
::_tcscpy_s(pArrayLabel[0], (LPCTSTR)szLabel0);
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
The
(LPCTSTR)
cast is superfluous there too, because the compiler will automatically call it when it sees that the_tcscpy()
parameter is anLPCTSTR
.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.
-
The
strcpy
below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.char **pArrayLabel = NULL; // I have to declare it this way, no vector.
// (It will be pass to another function later)int size=12;
pArrayLabel = new char*[size];CString szLabel0 = "Januari";
strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));You already got enough information from other replies. If you are using visual studio 2005, consider using secured version of these CRT function. Refer Security enhancement to CRT[^].
Prasad MS MVP - VC++