Warnings interfere with the program?

Asked

Viewed 115 times

3

I’m performing the following exercise: inserir a descrição da imagem aqui

After a few attempts I reached the following final code:

int proximo_da_media(int *vec, int dim){
 int i, *pos;
 float media, soma=0, diferenca;
 pos=vec; //inicializar ponteiro
  for(i=0;i<dim;i++)
    soma+=*(vec+i);  //faz a soma de todos os numeors
 media=0.5+(soma/dim); //faz a media e soma 0.5,como a seguir faz subtração com um int se a media tiver a parte decimal igual ou superior a 0.5 arredonda para cima se não mantem o int original
 diferenca=abs((*vec)-media); //calcula a diferença entre a media e o primeiro valor do vetor em valor absoluto
  for(i=0;i<dim;i++){
    if(abs(*(vec+i)-media)<diferenca){ //se o valor absoluto entre a media e vec[i] for menor que a diferenca anterior então foi descoberto um numero mais proximo da media logo atualizo a diferenca e o ponteiro para o numero encontrado
        diferenca=abs(*(vec+i)-media);
        pos=vec+i;
    }
}
return pos;}

int main(){
 int tabela[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 numbers is 2877 , making the average (2877/10) we get the result of 287,7 that rounds to 288, being 360 the number closest to the average.

I did some tests and the program has been running as expected, for this example also went well,got the following output: inserir a descrição da imagem aqui

Although I perform as expected I have 2 warnings and I do not know if I should change something in the code, the warnings are as follows: inserir a descrição da imagem aqui inserir a descrição da imagem aqui

I wonder if the algorithm is correct and if I should change something in the code.

2 answers

6


Yes.

And not.

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

The error will certainly give problem. The compiler has, and many people use, option that treats the warnings as errors, and it is generally recommended to do so anyway.

I hear inexperienced programmers consider that they do no harm. Until the day they do and the person is left without knowing what to do. And worse, many of the possible evils are unidentifiable.

Rule out all warnings. If there is any rare case that has nothing in normal code that can eliminate one of them, and it is very rare that this occurs, use a #pragma to silence the compiler and make it evident that there does something different.

The chance of these warnings causing trouble is great.

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

The second seems to be the consequence of the first mistake and if you fix it you must eliminate the second Warning.

I found the code confusing, it can be simpler than this, I think so gets 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I haven’t analyzed whether the code does what it expects.

  • Thanks for the explanation, I changed the int to int* and the warnings disappeared, however I also commented on my original code for easier understanding , really yours is more readable, I’ll make some changes but as I started learning pointers recently I was advised to use only pointers in my programs to get used to and understand well the manipulation of pointers

  • I don’t know if it was good advice.

  • Over the course of these days I have made several mistakes in the manipulation of the pointers that I will not commit again, I think it has helped me in this aspect

  • And probably picked up several vices... :)

1

Warning has already said what the problem is, you have defined the function return type as an int, but you are trying to return a pointer to int, change from int proximo_da_media(int *vec, int dim) for int* proximo_da_media(int *vec, int dim) and the message will be gone. In this case this Warning appeared, because it is not a syntax error but a static semantics error, and in C pointers it is the size of an int, in 64bit systems an int is worth 4 bytes and a pointer also has 4 bytes and what its function is returning is the value corresponding to the memory position where pos is pointing.

  • Thank you, I changed the function statement and the two warnings are gone!

Browser other questions tagged

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