简体   繁体   English

分配指向结构数组的指针

[英]Mallocing pointer to array of structs

Hi I'm trying to craft a HTTP response using some custom libraries.嗨,我正在尝试使用一些自定义库来制作 HTTP 响应。 The library function requires a pointer to an array of a custom struct HttpHeader.库函数需要一个指向自定义结构 HttpHeader 数组的指针。 Below the code is a snippet from the man page.代码下方是手册页中的一个片段。 I was wondering how to initialize it so that "Content-Length" populates the name and a value populates the value then the next HttpHeader in the array is a NULL pointer as specified by the man page.我想知道如何初始化它,以便“Content-Length”填充名称并填充值,然后数组中的下一个 HttpHeader 是手册页指定的 NULL 指针。 Below is the code I currently have but my system has an error when mallocing the original memory for headers.下面是我目前拥有的代码,但我的系统在为标头分配原始内存时出错。 "error: expected expression before 'HttpHeader' HttpHeader** headers = malloc(sizeof(**HttpHeader));" “错误:'HttpHeader' HttpHeader** headers = malloc(sizeof(**HttpHeader)) 之前的预期表达式;”

Any help would be greatly appreciated thanks!任何帮助将不胜感激谢谢!

 void populate_header(HttpHeader** headers, char* value) {
        headers[0]->name = malloc(sizeof(char) * strlen("Content-Length"));
        headers[0]->value = malloc(sizeof(char) * strlen(value));
        strcpy(headers[0]->name, "Content-Length");
        strcpy(headers[0]->value, value);
    }

char* process_address(char** addrContents) {
    HttpHeader** headers = malloc(sizeof(*HttpHeader));
    char* body = NULL;
    char* response = NULL;
    if (strcmp(addrContents[1], "validate") == 0) {
        populate_header(headers, "0");
        if (!check_expression(addrContents[2])) {
            response = construct_HTTP_response(400, "Bad Request", headers, body);
        } else {
            response = construct_HTTP_response(200, "OK", headers, body);
        }
    } else if (strcmp(addrContents[1], "integrate") == 0) {
        if (!check_expression(addrContents[2])) {
            populate_header(headers, "0");
            response = construct_HTTP_response(400, "Bad Request", headers, body);
        } else {

            response = construct_HTTP_response(200, "OK", headers, body);
        }
    } else {
        populate_header(headers, "0");
        response = construct_HTTP_response(400, "Bad Request", headers, body);
    }
    //printf("Response: %s\n", response);
    return response;
}

======================================== FROM MAN PAGE ======================================== 从手册页

headers
              points to an array of HttpHeader* (see below), each containing the name of value of a HTTP header. The last entry in headers will be a NULL
              pointer.

   HttpHeader
       A HttpHeader is defined as follows:
       typedef struct {
           char* name;
           char* value;
       } HttpHeader;
       Memory for name and value is allocated separately.

sizeof(*HttpHeader) is the problem. sizeof(*HttpHeader)是问题所在。 * dereferences a pointer. *取消引用一个指针。 HttpHeader is a type, it doesn't make sense to dereference a type. HttpHeader是一种类型,取消引用类型没有意义。

You instead want sizeof(HttpHeader*) .你想要sizeof(HttpHeader*) That's the type which is a pointer to a HttpHeader .这是指向HttpHeader的指针的HttpHeader

malloc(sizeof(HttpHeader*)); only allocates space for a single pointer.只为单个指针分配空间。 If you want to allocate space for more than one header, you need to multiply.如果要为多个头分配空间,则需要相乘。 For example, if you want space for five headers: malloc(sizeof(HttpHeader*) * 5);例如,如果您想要五个标头的空间: malloc(sizeof(HttpHeader*) * 5);

Finally, the array is supposed to be terminated with a null so it knows when to stop reading through the array.最后,数组应该以空值终止,以便它知道何时停止读取数组。 Allocate one more pointer than you need, and set the final element to null.分配比您需要的多一个指针,并将最后一个元素设置为空。

// Space for two headers, the last is a null.
HttpHeader** headers = malloc(sizeof(*HttpHeader) * 2);
headers[1] = NULL;

Similarly, strings in C are terminated with a null.类似地,C 中的字符串以 null 结尾。 You have to allocate one more byte than the length of the string.您必须比字符串的长度多分配一个字节。

sizeof(char) is defined to be 1, you can leave it out. sizeof(char)被定义为 1,你可以省略它。

void populate_header(HttpHeader** headers, char* value) {
  headers[0]->name = malloc(strlen("Content-Length") + 1);
  headers[0]->value = malloc(strlen(value) + 1);
  strcpy(headers[0]->name, "Content-Length");
  strcpy(headers[0]->value, value);
}

Better yet, use strdup .更好的是,使用strdup

void populate_header(HttpHeader** headers, char* value) {
  headers[0]->name = strdup("Content-Length");
  headers[0]->value = strdup(value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM