0

I am trying to make an array of structures in which each array elements itself points to another structure of the same type. In order to better explain the problem, I have made a schematic for it! The names of the variables are according to the code (shown below)

But I am getting an error while I am freeing the memory! Can someone provide some hints for solving this! Thanks.

array of structures

Error:

tempCodeRunnerFile.c:36:42: error: member reference type 'Vertex *' (aka 'struct Node *') is a pointer; did you mean to use '->'?
        Vertex* ptr = (*(vertexarray+ i)).next;
                      ~~~~~~~~~~~~~~~~~~~^
                                         ->
tempCodeRunnerFile.c:57:14: warning: incompatible pointer types passing 'Vertex [4]' to parameter of type 'Vertex **' (aka 'struct Node **') [-Wincompatible-pointer-types]
    finalize(vertexarray);
             ^~~~~~~~~~~
tempCodeRunnerFile.c:30:24: note: passing argument to parameter 'vertexarray' here
void finalize(Vertex** vertexarray){ // free the dynamic memmory 
                       ^
1 warning and 1 error generated.

Code:

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

// Data structure to store a linked list node
typedef struct Node
{   int data;
    struct Node* next;
}Vertex;

Vertex* initialize(); //Vertex** vertexarray
void finalize(Vertex** vertexarray);

Vertex* initialize(){ // initialise the elements inside the array
    
    Vertex* node = (Vertex*)malloc(sizeof(Vertex));

    if(node ==NULL){
        printf("Error in initialising"); // If node cannot be created due to memmory issues.
        return NULL; // listhead is NULL
    }
    else{
    node->data = -1; // The initial node is empty (contains data value -1, symbolising empty).
    node->next = NULL; // The list at the time of initialisation is empty and hence no node to further point on.
    }
    return node;

}

void finalize(Vertex** vertexarray){ // free the dynamic memmory 

    int i;

    for (i=0; i<4;i++){ // free all the memmory
        
        Vertex* ptr = (*(vertexarray+ i)).next; 
        Vertex* tmp;

        while( ptr!= NULL) // Termination condition: At the end, the pointer is not pointing to any node, but a NULL value
        {
            tmp = ptr; // make tmp the current node starting from listhead.
            ptr = ptr->next; // update the pointer to the next pointer
            free(tmp); // free the previous pointer
        }   
    }

}

int main(void)
{
    Vertex vertexarray[4];
    int i;

    for (i=0; i<4;i++){
        vertexarray[i].next = initialize();
    }
    finalize(vertexarray);
    return 0;
}
8
  • 1
    Either change *(vertexarray+ i) to vertexarray[i], or add parenthesis around it, in order to fix the operator precedence issue in your code: (*(vertexarray+ i)).next. Nice drawing BTW. Commented Oct 8, 2022 at 19:54
  • 1
    This appears as if you couldn't decide whether you wanted a dynamic pointer array, a dynamic array, or a fixed array, and ended up trying all three concurrently. Commented Oct 8, 2022 at 19:54
  • @WhozCraig Haha, yes! The problem statement was like that! Btw are you talking about the figure or the implementation? Because In the figure I will extend each array element to a dynamic linked list! Commented Oct 8, 2022 at 19:58
  • @bbbbbbbbb Sure, thanks for the suggestion! But I think I still need to fix some more things, since the code isn't running yet! Commented Oct 8, 2022 at 20:00
  • 1
    I'm talking about the implementation. That figure is pretty bad, though. It looks like they want a fixed value array, which chains from each value. Not what I would do, but apparently what they want. And fwiw, changing *(vertexarray+ i).next to vertexarray[i].next; (or (*(vertexarray+ i)).next) won't fix anything, including the compilation problem. All those are synonymous with what you started with. There isn't a precedence issue there whatsoever. Commented Oct 8, 2022 at 20:13

1 Answer 1

2

As the finalize method would need to free every element of the array you could write it like this

void finalize(Vertex vertexarray[], size_t len)
{ // free the dynamic memmory

  int i;

  for (i = 0; i < len; i++)
  { // free all the memmory

    Vertex *ptr = vertexarray[i].next;
    Vertex *tmp;

    while (ptr != NULL) // Termination condition: At the end, the pointer is not pointing to any node, but a NULL value
    {
      tmp = ptr;       // make tmp the current node starting from listhead.
      ptr = ptr->next; // update the pointer to the next pointer
      free(tmp);       // free the previous pointer
    }
  }
}

And call it in main as

finalize(vertexarray, 4);

It will still work if vertexarray will allocated dynamically as you still need to keep track of how many elements has. Unless you change the vertexarray to also be a list

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thanks a lot! Great help! Also, is there any way in which I can implement void finalize(Vertex** vertexarray) ? I couldn't really understand how to work with Vertex** vertexarray in my code! It will be really nice if you could share some of your thoughts on it! Thanks!
But why you want the argument to be Vertex** vertexarray? You need the argument to be an array of Vertex, right? You would need it to be Vertex **vertexarray if inside the function you would allocate memory that is accessed by *vertexarray. But it's not the case in your finalize
Sure, I understand that now! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.