获取(char *)func1.Version的值;使用c

时间:2019-03-14 16:43:32

标签: c

我需要在我的C代码中将char指针的变量内容复制到本地变量中。

我的值在(char*)BrakeBoardInfo.firmwareVersion;内部,我想使用c复制相同内容到局部变量中。

1 个答案:

答案 0 :(得分:1)

据我了解,您想复制一个字符串。

int NeededSize = strlen (BrakeBoardInfo.firmwareVersion);
char *MyCopy = (char*) malloc (NeededSize+1);
strcpy (MyCopy, BrakeBoardInfo.firmwareVersion);

* * * // do what you need with the variable

free (MyCopy);  //Don't forget to free the memory!

或者您可以避免动态内存分配:

char MyCopy[MAX_SIZE];  //Get MAX_SIZE value from documentation. It's the longest BrakeBoardInfo.firmwareVersion possible length + 1 null-term char.
strcpy (MyCopy, BrakeBoardInfo.firmwareVersion);
* * * // do what you need with the variable and you don't have to free memory after it.