Note that there are some explanatory texts on larger screens.

plurals
  1. POchar array is crashing my stack structure. Did I declare something wrong?
    primarykey
    data
    text
    <p>I'm trying to gain an intuition for how the stack structure works but for some reason any time I try to print from <code>charElements</code>, my program crashes and I have NO clue why. This is the error I keep getting: (its at a breakpoint) <code>while (i-- &amp;&amp; *p)</code>. But I have no clue whats wrong with how I declared everything. Any thoughts?</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;math.h&gt; typedef struct Stack { int capacity; // max # of elements the stack can hold int size; // current size of the stack int *elements; // the array of elements char *charElements; // the array of chars }Stack; Stack * createStack(int maxElements) { // Create a Stack Stack *S; S = (Stack *)malloc(sizeof(Stack)); // Initialise its properties S-&gt;charElements = (char *)malloc(sizeof(int)*maxElements); S-&gt;elements = (int *)malloc(sizeof(int)*maxElements); S-&gt;size = 0; S-&gt;capacity = maxElements; /* Return the pointer */ return S; } int main() { Stack *S = createStack(60); char registerNames[63] = {"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}; // if the user input a a string S-&gt;elements[S-&gt;size++] = 1; S-&gt;elements[S-&gt;size++] = 2; S-&gt;elements[S-&gt;size++] = 3; S-&gt;charElements[S-&gt;size++] = *registerNames; printf("%d \n", S-&gt;elements[0]); printf("%d \n", S-&gt;elements[1]); printf("%d \n", S-&gt;elements[2]); printf("%d \n", S-&gt;size); printf("%s \n", S-&gt;charElements[3]); system("pause"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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