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.
arr[i] = (int*)malloc(sizeof(int));doesn't make sense (assigning anint*to anintwithin an indeterminate array, thus invoking undefined behavior), andarr = node->id;makes equally no sense (assigningintto anint*. Those warnings should be treated as gospel errors, because that's exactly what they are.arrarray should be contiguous in memory. This implies a single call tomalloc, 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