简体   繁体   中英

trying to copy a char pointer using memcpy, getting an error

so I want to copy a char pointer, asked a friend and he said to use memcpy... so I am trying to do this:

charFilenameAndPath=strtok(filename,".");
memcpy=(charFilename,charFilenameAndPath, sizeof(charFilenameAndPath));

and the compiler is spitting out this:

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

I also tried using strlen instead of sizeof

memcpy is followed by a = and should not be. The error message says you are trying to assign a new value to the symbol memcpy , which isn't what you want to do.

In your second line:

memcpy=(charFilename,charFilenameAndPath, sizeof(charFilenameAndPath));

there is a spurious = sign.

Once you fix that, your call is not correct anyway. charFilenameAndPath is the return value from strtok() , so it must be a char * . So, you are copying sizeof(char *) bytes to charFilename , you probably want strlen(charFilenameAndPath)+1 bytes instead (or you can use strcpy() ). In any case, you should make sure that strtok() didn't return NULL and that charFilename has enough space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM