gpt4 book ai didi

c - 如何从结构中打印一个字符

转载 作者:太空狗 更新时间:2023-10-29 15:45:35 25 4
gpt4 key购买 nike

有人可以告诉我们在接收数据作为结构时打印一个字符吗?这是一个例子:

...
struct rcv{
int x1;
float x2;
char *x3;
};
rcv data_rcv;
...
if (recv(socket, &data_rcv, sizeof(data_rcv), 0) < 0)
printf("recv() failed");
...

printf("x1 = %d\n", data_rcv.x1);
printf("x2 = %f\n", data_rcv.x2);

printf("x3 = %s\n", data_rcv.x3); // it doesn't print anything, why?
...

假设 x3 是通过套接字传输的字符串,如下所示:

...
char *str1="data-of-app.1"

struct snd{
int x1;
float x2;
char *x3;
};
snd data_snd;
...
data_snd.snd = str1;
...
if (send(socket, &data_snd, sizeof(data_snd), 0) < 0)
printf("send() failed");
...

感谢您的回复-

最佳答案

printf("x3 = %s\n", data_rcv.x3);

应该是

printf("x3 = %c\n", data_rcv.x3);

关于c - 如何从结构中打印一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2429774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com