-5
struct PhaseShiftPin {
    int _PSPpin_index;      //PSP = Phase Shift Pin
    int _PSPvalue;
    char _PSPname[512];
};
struct PhaseShiftPin *_PhaseShiftPin[500000];
int p3int = 0;
strcpy ( _PhaseShiftPin[i]->_PSPvalue, p3int );

上面的代码只是我完整代码的一部分。当我编译我的完整程序时发生了一个错误strcpy' 从整数中生成指针而没有 在行中的强制转换

strcpy ( _PhaseShiftPin[i]->_PSPvalue, p3int );

在我尝试使用 strncpy 并遵循帖子中显示的方法后,我在这里引用了帖子从整数中生成指针而没有使用 strcpy进行转换,但我仍然无法编译。希望您能指导我解决我的问题。谢谢。

4

2 回答 2

2

strcpy is inappropriate for this problem. It takes two arguments. The first is a pointer to char, and the second is a pointer to const char. It then copies the string pointed to by the second pointer into the memory pointed to by the first.

In your case, you are not copying a string, you are copying an int. Your last line should be:

_PhaseShiftPin[i]->_PSPvalue = p3int;

However, the C99 standard reserves identifiers that begin with an underscore ('_') followed by an uppercase letter or another underscore for the implementation. Therefore you must not name your identifiers this way.

于 2013-07-23T07:04:14.033 回答
1

如果您查看 strcpy 的定义,您会看到

char *strcpy(char *dest, const char *src);

但是你的_PhaseShiftPin[i]->_PSPvalue也是一个整数p3int

你可以使用 memcpy

void *memcpy(void *dest, const void *src, size_t n);

strcpy ( &(_PhaseShiftPin[i]->_PSPvalue), &(p3int), sizeof(p3int) );

或者就像评论中所说的那样

_PhaseShiftPin[i]->_PSPvalue =  p3int;

如果您尝试复制p3int_PhaseShiftPin[i]->_PSPvalue

于 2013-07-23T07:02:15.713 回答