我正在做一个项目,由于某种原因,我无法strtol()
将 a 转换char *
为整数。我有这个代码:
int main(int argc, char *argv[]) {
//socket initialization code...
struct sockaddr_in addr;
char *buffer;
receivePacket(&sock, &buffer, &addr); //function i have written to receive a packet
char *temp;
int times = (int) strtol(buffer, &temp, 10);
}
当我运行它时,时间总是最终为 0,并且 temp 返回垃圾:
(gdb) print buffer
$1 = 0xbefff648 "1"
(gdb) print times
$2 = 0
(gdb) print temp
$3 = 0xbefff648 "H\366\377\276(\207"
我不明白为什么 strtol 无法理解我给它的东西。
在receivePacket
:
void receivePacket(int *sock, char **buffer, long *tx, struct sockaddr_in *cliaddr) {
//socket related code, initializations, etc.
char temp[10] = { 0 };
n = recvfrom(*sock, temp, 10, 0, (struct sockaddr *) &cliAddr, &clilen);
*buffer = temp;
}
这是问题所在吗?谢谢。