Skip to main content
deleted 14 characters in body
Source Link
jdt
  • 2.4k
  • 5
  • 20

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 2,147,483,647 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i or length is less than zero since it is unsigned.

Node* array_to_list(int32_tint array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        current->data = array[length - 1 - i];
        current->next = tail;
        tail =// current;...
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available. I would suggest something like the following:

Node* current = malloc(sizeof(Node));
if (current == NULL)
{
    fprintf(stderr, "Out of memory error");
    exit(EXIT_FAILURE);
}

Putting it all together:

Putting it all together:

Node* array_to_list(int32_tint array[], size_t length) {
    Node* tail = NULL;
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        if (!current == NULL)
        {
            fprintf(stderr, "Out of memory error");
            exit(EXIT_FAILURE);
        }
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

int main() {
    int array[] = { 1, 2, 3, 4, 5 };
    struct Node* list = array_to_list(array, sizeof(array) / sizeof(array[0]));
    for (Node* node = list; node; node = node->next)
        printf("%d\n", node->data);
    free_list(list);
}

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 2,147,483,647 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i or length is less than zero since it is unsigned.

Node* array_to_list(int32_t array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available. I would suggest something like the following:

Node* current = malloc(sizeof(Node));
if (current == NULL)
{
    fprintf(stderr, "Out of memory error");
    exit(EXIT_FAILURE);
}

Putting it all together:

Node* array_to_list(int32_t array[], size_t length) {
    Node* tail = NULL;
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        if (current == NULL)
        {
            fprintf(stderr, "Out of memory error");
            exit(EXIT_FAILURE);
        }
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 2,147,483,647 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i or length is less than zero since it is unsigned.

Node* array_to_list(int array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        // ...
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available.

Putting it all together:

Node* array_to_list(int array[], size_t length) {
    Node* tail = NULL;
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        if (!current)
        {
            fprintf(stderr, "Out of memory error");
            exit(EXIT_FAILURE);
        }
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

int main() {
    int array[] = { 1, 2, 3, 4, 5 };
    struct Node* list = array_to_list(array, sizeof(array) / sizeof(array[0]));
    for (Node* node = list; node; node = node->next)
        printf("%d\n", node->data);
    free_list(list);
}
added 500 characters in body
Source Link
jdt
  • 2.4k
  • 5
  • 20

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 2,147,483,647 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i or length is less than zero since it is unsigned.

Node* array_to_list(int32_t array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available. I would suggest something like the following:

Node* current = malloc(sizeof(Node));
if (current == NULL)
{
    fprintf(stderr, "Out of memory error");
    exit(EXIT_FAILURE);
}

The rest looks pretty solid.Putting it all together:

Node* array_to_list(int32_t array[], size_t length) {
    Node* tail = NULL;
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        if (current == NULL)
        {
            fprintf(stderr, "Out of memory error");
            exit(EXIT_FAILURE);
        }
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 2,147,483,647 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i or length is less than zero since it is unsigned.

Node* array_to_list(int32_t array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available. I would suggest something like the following:

Node* current = malloc(sizeof(Node));
if (current == NULL)
{
    fprintf(stderr, "Out of memory error");
    exit(EXIT_FAILURE);
}

The rest looks pretty solid.

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 2,147,483,647 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i or length is less than zero since it is unsigned.

Node* array_to_list(int32_t array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available. I would suggest something like the following:

Node* current = malloc(sizeof(Node));
if (current == NULL)
{
    fprintf(stderr, "Out of memory error");
    exit(EXIT_FAILURE);
}

Putting it all together:

Node* array_to_list(int32_t array[], size_t length) {
    Node* tail = NULL;
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        if (current == NULL)
        {
            fprintf(stderr, "Out of memory error");
            exit(EXIT_FAILURE);
        }
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}
added 29 characters in body
Source Link
jdt
  • 2.4k
  • 5
  • 20

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 42,294147,967483,294647 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i or length is less than zero since it is unsigned.

Node* array_to_list(int32_t array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available. I would suggest something like the following:

Node* current = malloc(sizeof(Node));
if (current == NULL)
{
    fprintf(stderr, "Out of memory error");
    exit(EXIT_FAILURE);
}

The rest looks pretty solid.

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 4,294,967,294 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i is less than zero since it is unsigned.

Node* array_to_list(int32_t array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available. I would suggest something like the following:

Node* current = malloc(sizeof(Node));
if (current == NULL)
{
    fprintf(stderr, "Out of memory error");
    exit(EXIT_FAILURE);
}

Use the correct types

Use size_t instead of int32_t for the array size and index. Using int32_t will limit it to arrays containing 2,147,483,647 elements even if you compiled it for 64-bit mode.

Also, note that it does not make sense to check if i or length is less than zero since it is unsigned.

Node* array_to_list(int32_t array[], size_t length) {
    // ...
    for (size_t i = 0; i < length; i++) {
        Node* current = malloc(sizeof(Node));
        current->data = array[length - 1 - i];
        current->next = tail;
        tail = current;
    }
    return tail;
}

Check if malloc was successful

You cannot simply assume that there will be enough memory available. I would suggest something like the following:

Node* current = malloc(sizeof(Node));
if (current == NULL)
{
    fprintf(stderr, "Out of memory error");
    exit(EXIT_FAILURE);
}

The rest looks pretty solid.

added 98 characters in body
Source Link
jdt
  • 2.4k
  • 5
  • 20
Loading
added 142 characters in body
Source Link
jdt
  • 2.4k
  • 5
  • 20
Loading
Source Link
jdt
  • 2.4k
  • 5
  • 20
Loading