I cannot assign a value to the last element of an array

Asked

Viewed 39 times

0

struct Numero{ ishort num; Paragraph *next; };

ishort array[3];
array[0] = grid1;
array[1] = grid2;
array[2] = grid3;
array[3] = grid4;

ishort i = 0;

Numero *n;
Numero *t;
Numero *h;

//lista circular
while(i <= 3)
{
    n = new Numero;
    n->num = array[i];

    if(i == 0)
    {t = n;
    h = n;}

    t->next = n; //linka com n
    t = t->next; //aponta pra n

    if(i == 3)
    {
        n->next = h;
        n = n->next;
    }

    i++;
}

i = 0;

while(i <= 3)
{
    cout << h->num << ", ";
    h = h->next;
    i++;
}

Simply put, I created a function that takes 4 values (e.g., 3, 5, 7 and 8) and assigns them in a 4-element array, to be stored in a circular list. During the storage loop, when array[i] arrives at time 3, it would receive array[3].

But at the time of the 2nd printing loop, print 3, 5, 7 and 0. I did everything right. I no longer know what I do, any help? Thanks!

  • ishort array[3]; the indexes of this array are: 0, 1 and 2. That way, you won’t be able to assign value to the input array[3].

  • thanks, I ended up going by a simple detail

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.