将整数值从字符串指针复制到C

时间:2015-10-02 00:43:56

标签: c pointers copy message-queue

我有一个字符串,我使用strtok

分成几部分
    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

2 个答案:

答案 0 :(得分:1)

您可以使用int

中的atoichar *中提取<stdlib.h>
outgoing.mtype = atoi(d.pid);

答案 1 :(得分:1)

要将字符串值转换为long int,可以使用

outgoing.mtype = atol(d.pid);