C : Printing a pointer to an array seems to print a junk value also -
I am writing a program to push and populate elements in a stack. My code is below,
#include & lt; Stdio.h & gt; #define MAX_STACK10 street_ar {int content [MAX_STACK]; Int top; }; Typewrift staff stack_are stack; Zero display_stack (stack * head) {int i; Printf ("% d -", (head-> content [i]) for {i = 0; i & lt; MAX_STACK; i ++) {if (i & lt; = head- )); Other printf ("N -"); } Printf ("\ n"); } Zero push (stack * head, int value) {if (head-> gt; top == - 1 & amp; amp; MAX_STACK! = 0) {head-> content [0] = value; Head & gt; Top = 0; } And if (((head-> top) +1) == MAX_STACK) printf ("stack full \ n"); Other major- gt; Content [++ head-> Top] = value; } Zero fill_stack (stack * head, int size, char ** content) {int i, value; For (i = 0; i Top! = - 1) {--head-> Top; Return head-> content [top-> top + 1]; } And printf ("no more elements left in the \ n stack \ n"); } Zero removal_stack (stack * head) {int i; (I = Head-> top; i>); i--) {printf ("\ n Stopping% d out of stack: \ n", pop (head)); Display_stack (head); }} Void main (int argc, char ** argv) {pile head; Fill_stack (& head, argc, argv + 1); Remove_stack (& amp; head); }
But in my output there are strange characters that comes like this,
$ ./stack.o 1/3
3 - N - N - N - N - N - N -Pushing in 3 to 15374463 - 1 - 2 - 3 - N - N - N - N -
Stack out of 3: 15774463 - 1 - 2 - N - N - N - N - N - N -
Except 2 of the stack: 15774463 - 1 - Stop the outside of the N - N - Stack: 15774463 - N - N - N - N - N - EN - N - N - -> - N - N - N - Blockquote>
Now I am not sure what is for 15774463.
Forgive my poor English skills, I hope what I want to ask if you don, I think what I should say, please ask me to understand.
Thank you.
You did not initialize the stack.
// Wrong, Unregistered! Stack head; // Start, C99 stack head = {.top = -1}; // Start, stack head for ancient compilers without support of C99; Head.top = -1;
The automatic variable in the function scope until you set its value.
Comments
Post a Comment