1

I'm trying to put integer data from each node in a queue into an array called arr. The data for each node is inputted by the user, say there's 5 nodes, and has 1 2 3 4 5, where node 1 stores a value of 1 and node 5 stores a value of 5. My code is below:

typedef struct linkedList {
  int val;
  struct linkedList *next;
} list;

I've tried doing the following but I'm getting a few warnings when I compile the code, I only included the body of the function and not the header or prototype and such.

  int i;
  int *arr;
  list *node = NULL;

  for(i = 0; i < 5; i++) {
    arr[i] = (int*)malloc(sizeof(int));
    arr = node->id;
    node = node->next;
  }

The errors that I get are: warning: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast and warning: variable ‘arr’ set but not used How do I actually get the data from the queue into an array so that the array is arr[0]=1 and arr[4]=5]? Thanks.

4
  • 3
    Those errors are fundamental. A good C text and exhaustive review of how pointers work, and how dynamic memory is managed, should probably be on your agenda. arr[i] = (int*)malloc(sizeof(int)); doesn't make sense (assigning an int* to an int within an indeterminate array, thus invoking undefined behavior), and arr = node->id; makes equally no sense (assigning int to an int*. Those warnings should be treated as gospel errors, because that's exactly what they are. Commented Apr 5, 2021 at 20:14
  • You're doing unnecessary allocations as well as not properly allocating to begin with, your code will result in undefined behavior/exceptions Commented Apr 5, 2021 at 20:14
  • 2
    Welcome to Stack Overflow! The biggest challenge your code faces, as addressed by @Nina, is that your arr array should be contiguous in memory. This implies a single call to malloc, so not in a loop. If you do that, then set the spot in the array, not the array itself, to be the next item, then you're set i.e. arr[i] = node->id. This leaves you with rigid, functional code, assuming everything else works Commented Apr 5, 2021 at 20:37
  • Also if your question involves code it's best to provide a Minimal, Reproducible, Example as documented here. stackoverflow.com/help/minimal-reproducible-example Commented Apr 5, 2021 at 20:44

1 Answer 1

2

Assuming you already have your linked list allocated correctly and you know the number of nodes (since you didn't post the code for your linked list) then your copy to array function should look like this.

int* copylist(struct LinkedList* ls,int numberofnodes){
    int* arr = malloc(sizeof(int)*numberofnodes);
    for(int index = 0; index < numberofnodes; ++index){
        arr[index] = ls->val;
        ls = ls->next;
    }
    return arr;
}
Sign up to request clarification or add additional context in comments.

Comments

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.