关于C中的structs,如何将struct中的数据复制到字符串数组中

问题描述

如果使用 mDNS 在网络上查找服务器,我有一个程序。它来自一个开源堆栈。 目前,我需要以下用例的指导。 用例:每当我运行 findServers 程序时,我计划添加一个额外的逻辑,尝试连接到提到的服务器,如果连接失败,我会打印一条警告消息,说服务器上的网络连接可能有问题。

对于代码

它具有如下定义的类型结构

typedef struct {
    size_t length; /* The length of the string */
    UA_Byte *data; /* The content (not null-terminated) */
} UA_String;

typedef struct {
    UA_UInt32 recordId;
    UA_String serverName;
    UA_String discoveryUrl;
    size_t serverCapabilitiesSize;
    UA_String *serverCapabilities;
} UA_ServerOnNetwork;

代码具有以这种方式运行的逻辑:

for(size_t i = 0; i < serverOnNetworkSize; i++) {
            UA_ServerOnNetwork *server = &serverOnNetwork[i];
            printf("Server[%lu]: %.*s",(unsigned long) i,(int) server->serverName.length,server->serverName.data);
            printf("\n\tRecordID: %d",server->recordId);
            printf("\n\tdiscovery URL: %.*s",(int) server->discoveryUrl.length,server->discoveryUrl.data);



            printf("\n\tCapabilities: ");
            /*for(size_t j = 0; j < server->serverCapabilitiesSize; j++) {
                printf("%.*s,",(int) server->serverCapabilities[j].length,server->serverCapabilities[j].data);
            }*/

            //added below
            printf("%.*s",(int) server->serverCapabilities[0].length,server->serverCapabilities[0].data);
            printf("\n\tStatus: ");
            printf("%.*s",(int) server->serverCapabilities[1].length,server->serverCapabilities[1].data);


            printf("\n\n");
        }

观察到的输出形式为

Server[0]: name1
    RecordID: 0
    discovery URL: opc.tcp://hostname2:4840
    Capabilities: LDSME-DESKTOPSIDE
    Status: Available

Server[1]: name2
    RecordID: 1
    discovery URL: opc.tcp://hostname:4842
    Capabilities: Crane
    Status: Starting...

Server[2]: hostname
    RecordID: 2
    discovery URL: opc.tcp://hostname:4840
    Capabilities: LDSME-NOTEBOOKSIDE
    Status: AVailable

这将是认情况。但我计划 ping 每个提到的 URL(或尝试发送消息)以检查网络是否一切正常。 所以我打算提取URL信息。

因此我声明了一个字符数组A,并尝试使用strcpy和memcpy函数将server->discoveryURL.data中的内容复制到数组A中。

但它失败了。

for(size_t i = 0; i < serverOnNetworkSize; i++) {
           UA_ServerOnNetwork *server = &serverOnNetwork[i];
           strcpy(A[i],server->discoveryUrl.data);
           printf("URL %d: %s\n",A[i]);

        }

它失败了,甚至没有通过循环。需要一些指导才能获得以下格式的输出

URL 0 : opc.tcp://hostname2:4840
URL 1 : opc.tcp://hostname:4842
URL 2 : opc.tcp://hostname:4840

我也不明白为什么在结构字符串的 printf 语句中,“%s”在末尾给出了一个额外的字符,而“%.*s”给出了正确的输出。请期待指导。

编辑:我稍微修改代码并引入了一个新的全局字符数组,并使用了 memcpy 函数。但是我很挣扎,因为我在 URL 字段中得到了一个额外的字符。

char *A[] = {};
int main(){

for(size_t i = 0; i < serverOnNetworkSize; i++) {
           UA_ServerOnNetwork *server = &serverOnNetwork[i];        
           A[i] = (char*)UA_malloc(server->discoveryUrl.length+1); 
       memcpy(A[i],server->discoveryUrl.data,server->discoveryUrl.length);  
           printf("URL %d: %.*s\n",A[i]);
        }
}

输出被视为:

URL 0: opc.tcp://o755-gksr:48401
URL 1: opc.tcp://o755-gksr:48421

末尾有一个额外的字符 1,这是错误的。请提供有关如何处理的任何指导。

解决方法

复制后的字符串数据需要以'\0'结尾。 这样 printf 的 %s 就会知道什么时候停止:

// first we need some memory for the destination of the copy
A[i] = malloc(server->discoveryUrl.length+1); // +1 for the '\0' at the end
// then we copy exactly the number of characters we want
strncpy(A[i],server->discoveryUrl.data,server->discoveryUrl.length);
// then we add the '\0' terminator
A[i][server->discoveryUrl.length] = '\0'; // add the '\0' at the end

这是一个示例:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>

// defien these so we don't need mDNS
typedef char UA_Byte;
typedef uint32_t UA_UInt32;

typedef struct {
    size_t length; /* The length of the string */
    UA_Byte *data; /* The content (not null-terminated) */
} UA_String;

typedef struct {
    UA_UInt32 recordId;
    UA_String serverName;
    UA_String discoveryUrl;
    size_t serverCapabilitiesSize;
    UA_String *serverCapabilities;
} UA_ServerOnNetwork;

// for testing we only need one of these
UA_ServerOnNetwork serverOnNetwork[1] = {
    {
        1,// recordId
        {3,"abcd"},// serverName
        {3,"efgh"},// discoveryUrl
        0,// serverCapabilitiesSize
        NULL        // serverCapabilities
    }
};
int serverOnNetworkSize = sizeof(serverOnNetwork)/sizeof(serverOnNetwork[0]);

int main() {

    // first print it using the %.*s printf formatter
    for(size_t i = 0; i < serverOnNetworkSize; i++) {
        UA_ServerOnNetwork *server = &serverOnNetwork[i];
        printf("Server[%lu]: %.*s",(unsigned long) i,(int) server->serverName.length,server->serverName.data);
        printf("\n\tRecordID: %d",server->recordId);
        printf("\n\tDiscovery URL: %.*s",(int) server->discoveryUrl.length,server->discoveryUrl.data);
    }

    // now make an array of '\0' terminated strings,fill them in,and print them
    printf("\n\nURLs:");
    char *A[serverOnNetworkSize];
    for(size_t i = 0; i < serverOnNetworkSize; i++) {
        UA_ServerOnNetwork *server = &serverOnNetwork[i];
        // first we need some memory for the destination of the copy
        A[i] = malloc(server->discoveryUrl.length+1); // +1 for the '\0' at the end
        // then we copy exactly the number of characters we want
        strncpy(A[i],server->discoveryUrl.length);
        // then we add the '\0' terminator
        A[i][server->discoveryUrl.length] = '\0'; // add the '\0' at the end
        // and finally we print it
        printf("\n\tURL %lu: %s\n",A[i]);
    }

    return 0;
}

在这里试试:https://godbolt.org/z/cbcsdG