简体   繁体   中英

Mallocing pointer to array of structs

Hi I'm trying to craft a HTTP response using some custom libraries. The library function requires a pointer to an array of a custom struct 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. 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));"

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. * dereferences a pointer. HttpHeader is a type, it doesn't make sense to dereference a type.

You instead want sizeof(HttpHeader*) . That's the type which is a pointer to a 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);

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. 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.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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