2015-10-02 27 views
0

我有我拆成小块用strtok复制整数值字符指针到局部变量用C

d.dtype = strtok(incoming.mtext, "|"); 
    d.threshold= strtok(NULL, "|"); 
    d.pid = strtok(NULL, "|"); 

使用

printf("device type %s\n", d.dtype); 
    printf("device threshold %s\n", d.threshold); 
    printf("device pid %s\n", d.pid); 

我所看到的一切是正确指定的字符串。

我然后发送传出消息,我的消息结构是

struct msg_st { 
    long int mtype; 
    char mtext[BUFSIZ]; 
}; 

struct msg_st outgoing; 

我怎样才能在d.pid值复制到我的outgoing.mtype

+1

'outgoing.mtype =与strtol(d。 pid,NULL,10);' – chux

回答

1

您可以通过使用atoi<stdlib.h>

outgoing.mtype = atoi(d.pid); 
+0

我可以发誓我试过了,它给了我一个错误,虽然它似乎已经奏效,谢谢。 –

+0

很高兴知道。不要忘记点击接受答案按钮。谢谢! – Xoul

1

提取char *int值要转换为字符串值长整型,你可以用

outgoing.mtype = atol(d.pid);