Note that there are some explanatory texts on larger screens.

plurals
  1. POPointer to pointer confusion
    text
    copied!<p>Edit: The reason queue is 2d is because I need a pointer of Command so that cmd can equal NULL. NULL == (void *). This is where I get confused though, and why I've come here. :)</p> <p>To help try and figure out another problem I have in Python, I'm implementing a small test program in C. While I know a little, apparently I'm confused. I'm trying to write a simple queue to be used in asynchronous USB transfers. Something's not right with queue, as every command popped from the queue is the same. If I write queue[1024][0] as queue[1024][1] instead, the command alternates between two distinct commands, and the program crashes in command_thread_main. Apparently it doesn't notice that cmd should be NULL. Changing [1] any higher has no effect as far as I can tell. Any hints?</p> <pre><code>typedef struct Command { void (*cb) (char *data, int size); unsigned char *data; int size; } Command; struct Command queue[1024][0]; int queueEnd = 0; int queueStart = 0; static void queue_push(void (*cb), unsigned char *data, int size) { if (queueEnd &gt;= 1024) return; queue[queueEnd]-&gt;cb = cb; queue[queueEnd]-&gt;data = data; queue[queueEnd]-&gt;size = size; queueEnd++; } struct Command * queue_pop(void) { if( queueStart &gt; queueEnd ) return NULL; return queue[queueStart++]; } static void *command_thread_main(void *arg) { struct Command *cmd; while (!do_exit) { if(locked) continue; locked = 1; cmd = queue_pop(); if(cmd != NULL) cmd-&gt;cb(cmd-&gt;data, cmd-&gt;size); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload