Note that there are some explanatory texts on larger screens.

plurals
  1. POnot sure what's going on in this code
    text
    copied!<p>I have some C code, and I'm not quite sure what's going on.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define DIM1 7 #define DIM2 5 #define RES_SIZE 1000 typedef double stackElementT; typedef struct { stackElementT *contents; int maxSize; int top; int min2; } stackT; void StackInit(stackT *stackP, int maxSize) { stackElementT *newContents; newContents = (stackElementT *)malloc(sizeof(stackElementT)*maxSize); if (newContents == NULL) { fprintf(stderr, "Not enough memory.\n"); exit(1); } stackP-&gt;contents = newContents; stackP-&gt;maxSize = maxSize; stackP-&gt;top = -1; } void StackDestroy(stackT *stackP) { free(stackP-&gt;contents); stackP-&gt;contents = NULL; stackP-&gt;maxSize = 0; stackP-&gt;top = -1; } int StackIsEmpty(stackT *stackP) { return stackP-&gt;top &lt; 0; } int StackIsFull(stackT *stackP) { return stackP-&gt;top &gt;= stackP-&gt;maxSize-1; } void StackPush(stackT *stackP, stackElementT element) { if(StackIsFull(stackP)) { fprintf(stderr, "Can't push element: stack is full.\n"); exit(1); } stackP-&gt;contents[++stackP-&gt;top] = element; } stackElementT StackPop(stackT *stackP) { if(StackIsEmpty(stackP)) { fprintf(stderr, "Can't pop element: stack is empty.\n"); exit(1); } return stackP-&gt;contents[stackP-&gt;top--]; } int shell(char* s1, int arg) { printf("&gt; "); scanf("%s %d%*c", &amp;s1, &amp;arg); return arg; } int main() { char cmds[DIM1][DIM2] = {{"push"}, {"pop"}, {"add"}, {"ifeq"}, {"jump"}, {"print"}, {"dup"}}; char* s1; int arg; arg = shell(s1, arg); printf("%s\n", &amp;s1); } </code></pre> <p>Input: <code>push 4</code>. It prints <code>J+</code> instead of "push" but prints <code>4</code> normally.</p> <p>It also gives these warnings on compile:</p> <pre><code>stack.c: In function ‘shell’: stack.c:60: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’ stack.c: In function ‘main’: stack.c:71: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’ stack.c:65: warning: unused variable ‘cmds’ stack.c:69: warning: ‘arg’ is used uninitialized in this function </code></pre> <p>Can someone please explain?</p>
 

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