试图使用memcpy复制一个char指针,出现错误
所以我想复制一个字符指针,问一个朋友,他说使用memcpy ...所以我试图做到这一点:
charFilenameAndPath=strtok(filename,".");
memcpy=(charFilename,charFilenameAndPath, sizeof(charFilenameAndPath));
编译器正在吐出这个:
uTrackSpheres.cpp:176: error: assignment of function ‘void* memcpy(void*, const void*, size_t)’ uTrackSpheres.cpp:176: error: cannot convert ‘unsigned int’ to ‘void*(void*, const void*, size_t)throw ()’ in assignment
我也尝试使用strlen而不是sizeof
在你的第二行:
memcpy=(charFilename,charFilenameAndPath, sizeof(charFilenameAndPath));
有一个寄生=标志。
一旦你解决了这个问题,你的电话无论如何都是不正确的。 charFilenameAndPath是strtok()的返回值,所以它必须是char * 。 因此,您将sizeof(char *)字节复制到charFilename ,您可能需要strlen(charFilenameAndPath)+1字节(或者可以使用strcpy() )。 无论如何,你应该确保strtok()没有返回NULL ,并且charFilename有足够的空间。
memcpy后跟a = ,不应该是。 该错误消息表示您正试图为符号memcpy分配一个新值,这不是您想要执行的操作。
上一篇: trying to copy a char pointer using memcpy, getting an error
下一篇: What are all the common undefined behaviours that a C++ programmer should know about?
