我正在尝试将PIN(PIN内有数据)复制到temp。(假设销始终小于温度)

const char * PIN;
....
char [10] temp  ="";
int i = 0;
while (*(PIN+i)) {
    temp[i] = (PIN+i)*;
    i++;
}

如果我把temp当作一个指针来处理,也可以吗?
long int res = strtol (&temp, NULL, 10);

最佳答案

一些事情,
temp=“\0”
实际上将在字符串中放入两个0,一个是您编写的,另一个是由引号暗示的。
PIN+i需要被解引用(用*表示),否则它将一直持续下去,除非您能保证地址空间末尾有一个空指针。

while (*(PIN+i))

然后要复制字符而不是指针,还应该取消对赋值语句的引用。
temp[i]=*(PIN+i);
i++; // Because the pointer needs to be incremented

你为什么不直接用strcpy呢?还是史崔西?

关于c - 复制字符串时可以工作吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9074422/

10-11 22:33
查看更多