将整数值从 Char 指针复制到 C 中的局部变量

Copying Integer Value From Char Pointer to Local Variable in C

我有一个字符串,我使用 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

您可以使用 <stdlib.h>

中的 atoichar * 中提取 int
outgoing.mtype = atoi(d.pid);

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

outgoing.mtype = atol(d.pid);