Use strcpy with mxArrayToString ?
古いコメントを表示
I am trying to make a deep copy of the string from char pointer using strcpy from standard library. But it is causing the program to crash
% test2.c
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray const *prhs[])
{
char* inputStr = mxArrayToString(prhs[0]);
char* inputStr2 = strcpy(inputStr2, inputStr); % Not working and causing matlab to crash
mexPrintf("Input: %s\n", inputStr); % Working as expected
mexPrintf("Input: %c\n", inputStr[2]);
char s = inputStr[2];
mexPrintf("Copy: %c\n", s);
mexPrintf("Copy: %s\n", inputStr2); % Not working and causing matlab to crash
}
With some testing, I can access the inputStr pointer using mxArrayToString, I can also access individual elements of the char that inputStr points to. But I can not use inputStr as pointer to perform a deep copy. My guess is it is probably due to some mechanism within mxArray class that protects itself from being manipulated in unexpected way, but couldn't figure out the details
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Write C Functions Callable from MATLAB (MEX Files) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!