Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't want to dynamically size the array, another approach is to allocate an array with MAXSIZE elements, where MAXSIZE is "big enough". Also, some other comments:</p> <p>You declared at the top of your program a character array of size 1:</p> <pre><code>char cont[] = { 'y' }; </code></pre> <p>But in your scanf line later you try to use this:</p> <pre><code>s("%s", &amp;cont[0]); </code></pre> <p>This will overflow your buffer even if the user types just one character, because %s assumes that the buffer has at least two bytes available, one for the character and one for the '\0'. Possible fix: </p> <pre><code>char cont[] = { 'y', '\0' }; // ... s("%1s", cont); </code></pre> <p>Note that cont is the same and more common way of saying &amp;cont[0]. </p> <p>Another problem is with things that might be caught by the compiler if warnings are turned on: all functions should be prototyped before they are mentioned, functions without explicit return type should be declared with type int, and you should not let a function drop off without returning a value even though you declared it to explicitly or implicitly. Also, '(' and ')' do not need to be escaped in string literals.</p> <p>Here is a modified version with changes noted. I have redefined clrscr() because I don't have conio.h on this system:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;ctype.h&gt; // #include &lt;conio.h&gt; #define clrscr() printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdbool.h&gt; #define p printf #define s scanf #define MAXSIZE 500 // prototypes [Changed] int pop(); void push(int b); int pick(); int view(); int top; int ar[MAXSIZE]; int size; int main() { int opt, num; char cont[] = { 'y', '\0' }; // [Changed] clrscr(); p("Stacking Program\n\n"); // keep asking until we get a valid size [Changed] for (;;) { p("Data Size: "); s("%d", &amp;size); if (size &gt; 0 &amp;&amp; size &lt; MAXSIZE) break; printf("Not a valid size!\n"); } p("\n"); while((cont[0] == 'y') || (cont[0] == 'Y')) { clrscr(); p("Stacking Program"); p("\n\nData Size: %d\n\n", size); p("MAIN MENU\n1. Pop\n2. Push\n3. Pick\n4. View\nChoose: "); s("%d", &amp;opt); p("\n"); switch(opt) { case 1: pop(); break; case 2: if(top &gt; size) { p("You can't push more data"); } else { p("Enter data for Data[%d]: ", top); s("%d", &amp;num); push(num); } break; case 3: pick(); break; case 4: view(); break; default: p("Your choice is not in the list."); break; } p("\n\nDo you want continue(Y/N)?"); s("%1s", cont); // [Changed] } return 0; } int pop() { int a; if(top == 0) // [Changed] { p("Stack empty."); return 0; } else { top--; // [Changed] a = ar[top]; p("(Data[%d] = %d) removed.", top, a); return a; // [Changed] } } void push(int b) { ar[top] = b; top++; // [Changed] } int pick() { if(top == 0) // [Changed] { p("Nothing to display."); return 0; } else { p("(Data[%d] = %d) is the last data.", top, ar[top-1]); // [Changed] return -1; // [Changed] } } int view() { int i; if(top &lt; 0) { p("Nothing to display."); return 0; } else { for(i = 0; i &lt; top; i++) // [Changed] { p("Data[%d] = %d\n", i, ar[i]); } return -1; // [Changed] } } </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.
    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