Calculate vector size

Asked

Viewed 38,081 times

1

How to calculate the vector size?

My code:

#include <stdio.h>
#include <stdlib.h>

int retornaMediana(int *vetor){

int tam=0;

    tam = sizeof(vetor);

    printf("TESTE: %d, %d, %d\n\n",vetor[0],vetor[1],vetor[2]);

    return tam;

}

void main(){

int vetor[] = {5,25,7,10,13,33,45,11,60};
int n = retornaMediana(&vetor);
int n2;

n2 = sizeof(vetor)/sizeof(int);

    printf("TAMANHO = %d\n\n",n);
    printf("TAMANHO = %d\n\n",n2);


}

How do I get the function to return the real size of the vector ? I’ve passed the parameter, reference and nothing, I only get this 4 as a response.

  • And what result do you expect? Because?

  • What is the purpose of n2 = sizeof(vetor) / sizeof(int);?

  • Please do not change the code in the question after it has been asked - one of the answers points to parts of the code that have changed and this makes it very difficult to keep track of anything.

  • This function was to return tam, being the size of the vector, that is, a function that received a vector, and returned its size.

  • N2 was just a test in main to test if sizeof worked.

  • Do you want the vector size in bytes? That’s it?

  • I advise a reading of c-Faq, especially section 6.

Show 2 more comments

2 answers

3

This line is calculating the size correctly:

int len = sizeof(vetor)/sizeof(int);

Better would be

int len = sizeof(vetor)/sizeof(vetor[0]);

because, in case you change the type of vetor, will not be surprised by a bug difficult to perceive depending on the context.

Important to understand that the sizeof is already solved by the compiler, because he simply already knows what was done in the variable definition, so if you need to know the size of a vector passed by parameter, the solution that remains is to always pass the vector and the size.

Back to your code, the problem is that you are calculating the size correctly, but you end up overwriting the value on this other line, this time with the pointer size:

tam = sizeof(vetor);

An output would be to calculate the size within the declaration scope, as you were already doing, and send the size to the median function.

See the difference:

#include <stdio.h>
#include <stdlib.h>

int retornaMediana(int *vetor, int tam){
    // -- aqui você corre os ítens e calcula a mediana --
    // return mediana;
    printf("TESTE: %d, %d, %d\n\n",vetor[0],vetor[1],vetor[2]);
    return tam;
}

void main(){
    int vetor[] = {5,25,7,10,13,33,45,11,60};
    int len = sizeof(vetor)/sizeof(vetor[0]);

    int n  = retornaMediana( vetor, len );

    printf("TAMANHO = %d\n\n",n);
    printf("TAMANHO = %d\n\n",len);
}

Here’s a demonstration on IDEONE.

  • but let’s assume that I want a function that receives only one vector, and returns its size..

  • Always you will need to pass the size. The sizeof works at the time of compilation.

0

The vector inside the method is now a pointer, so the size will always be 8 (if your file is 64x), to calculate its size, you must create a method that traverses the pointer and count the positions.

int vec_len(int *vec){
    int c = -1;
    while(*(vec++))
        c++;
    return c;
}

I recommend you use some value like 0 to identify when to arrive at the end of the vector.

  • 1

    This doesn’t make any sense.

  • 1

    Of course it does, how do you get the size of a pointer? Just traversing to the final position, the sizeof on a pointer always returns a specific number, and I don’t think there’s anything ready in C that takes how many items are inside a pointer.

  • And how do you know when is the last element of the pointer?

  • So this is the missing part of the answer - the programmer has to agree on an element that will be the "end marker" - in this case, he has an implicit agreement that the ovector ends in the first element of value 0 - but other than that, this is the correct rspotsa, and the other is that it doesn’t make any sense. What has to be clear for OP is that in C, there is no associated metadata there is an array other than aqeules that you explicitly associate, , or explicitly count the size of the vector in another variable.

  • is a problem kind of unsolved then, if we have a function that receives ONLY the vector, and returns its size.

Browser other questions tagged

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