Vector by Parameter in C

Asked

Viewed 68 times

-1

I need to pass the Vector position by parameter as follows code, my function receives the address and modifies directly the variable passed by parameter. But the program stops working and closes during the build. Can anyone help me??

for(i=10;i>=0;i++)
    FPVazia(&Buckets[i]);

void FPVazia(TipoPilha *Pilha)
  • Always include the code, not an image with it. I edited the question.

  • 4

    The loop never stops. It starts at 10 and always adds one, never approaching the stopping condition

2 answers

0

When working with vectors, you should not put & to pass the memory address because it already receives this way (only with vectors).

Put the rest of the code because you are passing a vector element, but in the method signature it receives a stack data structure.

Your code can be like this:

for(i = 9; i >= 0; i--)
    FPVazia(Buckets[i]);

Or so:

for(i = 0; i < 10; i++)
    FPVazia(Buckets[i]);

0

In this case it is not necessary to put the & before the vector, let’s analyze why, its function is the following void FPVazia(TipoPilha *Pilha) note that what you ask for as a parameter is a pointer to a memory region, this one will be interpreted as a Pilla type, and when you call it you are passing the following, FPVazia(&Buckets[i]);, and because this is wrong, here we come to the definition of what is vector, a vector is a pointer to a memory region, this would be its first memory region of vector, index 0.

So to facilitate your understanding, for you to better understand what you’ve done, another way to write what you’ve done is this FPVazia(&(TipoPilha*)(Buckets + i));, soon you notice that what you are passing to your function is the memory address of your pointer and not the position of your vector, because a vector is already a pointer.

To fix this just take this &, being as follows FPVazia(Buckets[i]); or FPVazia((TipoPilha*)(Buckets + i));, it is important to note that these two ways are equivalent, the only difference between them is that one is omitted the pointer and the position account, while the other is not.

The second way is done using pointer arithmetic, a pointer * by itself is always interpreted as an integer in C, so to avoid that any error always occurs when using pointer arithmetic remember to cast, so that the pointer is interpreted according to the type you are using, if the variable Buckets were an integer vector you could leave only FPVazia(*(Buckets + i));, but as it is not do the casting. Another important fact is that as already said previously you do Buckets[i] or (TipoPilha*)(Buckets + i) is the same thing, they are equivalent, because when you do Buckets[i] what the computer interprets and does is (TipoPilha*)(Buckets + i).

Another error that is explicit is that of for, you start your i in 10, and then increases it, only that its condition says that to i >= 0 you will do what is inside the for, but this condition will always be true soon your loop will be infinite, to arrange this just decrease your i, or you start the i in 0, in the condition i < 10, and hence increases the i.

Going straight to the answer, you can do the following:

for ( i = 10 ; i >= 0; i-- )
    FPVazia(Buckets[i]);

or

for ( i = 10 ; i >= 0 ; i-- )
    FPVazia((TipoPilha*)(Buckets + i));

or

for ( i = 0 ; i < 10; i++ )
    FPVazia(Buckets[i]);

or

for ( i = 0 ; i < 10 ; i++ )
    FPVazia((TipoPilha*)(Buckets + i));

Browser other questions tagged

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