Warnings interfere with the program?

3

I am performing the following exercise:

Afterafewattempts,Icameupwiththefollowingfinalcode:

intproximo_da_media(int*vec,intdim){inti,*pos;floatmedia,soma=0,diferenca;pos=vec;//inicializarponteirofor(i=0;i<dim;i++)soma+=*(vec+i);//fazasomadetodososnumeorsmedia=0.5+(soma/dim);//fazamediaesoma0.5,comoaseguirfazsubtraçãocomumintseamediativerapartedecimaligualousuperiora0.5arredondaparacimasenãomantemointoriginaldiferenca=abs((*vec)-media);//calculaadiferençaentreamediaeoprimeirovalordovetoremvalorabsolutofor(i=0;i<dim;i++){if(abs(*(vec+i)-media)<diferenca){//seovalorabsolutoentreamediaevec[i]formenorqueadiferencaanteriorentãofoidescobertoumnumeromaisproximodamedialogoatualizoadiferencaeoponteiroparaonumeroencontradodiferenca=abs(*(vec+i)-media);pos=vec+i;}}returnpos;}intmain(){inttabela[10]={20,30,43,5,400,1999,9,360,3,8},*posicao;posicao=proximo_da_media(tabela,10);printf("O endreco do ponteiro que aponta para o numero mais proximo da media e o %p e tem o valor %d\n",posicao,*posicao);}

With the vector inserted in the code the sum of all the numbers is 2877, doing the average (2877/10) we got the result of 287.7 that rounds to 288, 360 being the closest number of the average. p>

I made some tests and the program has run as expected, for this example also went well, I got the following output:

WhilerunningasexpectedIhave2warningsandI'mnotsureifIshouldchangeanythinginthecode,thewarningsareasfollows:

I would like to know if the algorithm is correct and should I change something in the code.

    
asked by anonymous 31.03.2018 / 19:16

2 answers

6

Yes.

And no.

That is, potentially interferes. Do you accept something that may or may not be a problem?

The error will certainly give you trouble. The compiler has, and many people use, an option that treats warnings as errors, and it is usually recommended to do so anyway.

I hear inexperienced programmers consider that they do not cause harm. Until the day that causes and the person is not knowing what to do. And worse, many of the possible evils are not identifiable.

Have as a rule delete all warnings . If you have a rare case that you do not have anything in normal code that can delete one of them, and this is very rare, use a #pragma to silence the compiler and make it obvious that it does something different there.

The chance of these warnings causing a problem is great.

The first one should practically be a mistake. If you want to return a pointer, make the function type be a pointer, and then use the result appropriately. In fact, from what I understand from the code, it is an error to return int , it should be int * .

The second seems to be a consequence of the first error and if it fixes it should eliminate the second warning .

I found the code confusing, it may be simpler than this, I think it looks better:

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

int *proximo_da_media(int *vec, int size) {
    int *pos = vec;
    float soma = 0;
    for (int i = 0; i< size; i++) soma += pos[i];
    float media = soma / size + 0.5f;
    float diferenca = abs(*pos - media);
    for (int i = 0; i < size;i++) {
        if (abs(pos[i] - media) < diferenca) {
            diferenca = abs(pos[i] - media);
            vec = &pos[i];
        }
    }
    return vec;
}

int main() {
    int tabela[10] = {20, 30, 43, 5, 400, 1999, 9, 360, 3, 8};
    int *posicao = proximo_da_media(tabela, 10);
    printf("O endreco do ponteiro que aponta para o numero mais proximo da media e o %p e tem o valor %d\n", (void *)posicao, *posicao);
}

I did not analyze if the code does what it expects.

    
31.03.2018 / 19:27
1

The warning has already stated the problem, you defined the return type of the function as an int, but you are trying to return a pointer to int, change from int proximo_da_media(int *vec, int dim) to int* proximo_da_media(int *vec, int dim) and the message will disappear. In this case, this warning appeared because it is not a syntax error, but a static semantic, and in C pointers is the size of an int, in 64bits systems an int is 4 bytes and a pointer also has 4 bytes and what its function is returning is the value corresponding to the memory location where pos is pointing.

    
31.03.2018 / 19:26