I'm stuck in some trouble, unfortunately I'm still learning the details of this language.
I have the following code
#define SIZE 5
typedef struct
{
int start;
int end;
int nElements;
int vetor[SIZE];
} TSTACK;
and I try to inser values in its array using the following function:
void Push(TSTACK *s, int elementInsert)
{
if(IsFull(s))
{
printf("%s\n", "# ERROR: full.");
}
else
{
s->end++;
if(s->end == SIZE)
{
s->end = 0;
}
s->vetor[s->end] = elementInsert;
s->nElements++;
}
}
and I use the following function to show elements:
void Display(TSTACK *s)
{
int i;
if (isEmpty(s))
{
printf("%s\n", "# ERROR: empty");
}
else
{
for (i = 0; i < s->nElements; i++)
{
printf ("value: %d\n", s->vetor[i]);
}
}
}
My question is that I don't get the same result when I add different amount of elements, when I insert 5 elements, the Display function display all the elements correctly, but when I insert less than 5, the first item appears to be its pointer:
The following main results in 4201166, 3, 5, 7
int main(void)
{
TSTACK test;
test.start = 1;
test.end = 0;
test.nElements = 0;
Push(&test, 3);
Push(&test, 5);
Push(&test, 7);
Push(&test, 3);
Display(&test);
}
The following main results in 5, 3, 5, 7, 3
int main(void)
{
TSTACK test;
test.start = 1;
test.end = 0;
test.nElements = 0;
Push(&test, 3);
Push(&test, 5);
Push(&test, 7);
Push(&test, 3);
Push(&test, 5);
Display(&test);
}
Can someone show me where I am wrong?