Warnings when compiling the program

Asked

Viewed 763 times

0

In the code below, I need to create a structure for Point (Coordinate x and y) and create a function that creates these points using pointers. The structure was made as follows:

typedef struct //Estrutura definida para os pontos.
{
    double x; //Coordenadas X e Y.
    double y;
} Ponto;

Then I created the function to create these dots and another function to print those same dots:

void CriaPonto(Ponto *p, double x, double y) //Função que cria n pontos.
{
    p->x = x;
    p->y = y;
    int i, nponto;
    printf("Digite a quantidade de pontos que se quer (Maximo 100): ");
    scanf("%d", &nponto);
    if(nponto < 1 || nponto > 100)
    {
        printf("Quantidade de pontos invalida.");
    }
    else
    {
        printf("Digite as coordenadas X e Y:\n");
        for(i = 0; i < nponto; i++)
        {
            scanf("%lf", &p[i].x);
            scanf("%lf", &p[i].y);
        }
        printf("\n");
    }
}

void ImprimePonto(Ponto P[])
{
    int i, nponto;
    for(i = 0; i < nponto; i++)
            printf("Ponto %d: (%.0lf,%.0lf)\n", i+1, P[i].x, P[i].y);
    printf("\n");
}

In the main (main) function of the program I did as follows:

int main()
{
    Ponto Ponto[MAX];

    int x, y;

    CriaPonto(Ponto, x, y);
    ImprimePonto(Ponto);
    return 0;
}

When compiling I get 3 warnings.

In function 'ImprimePonto'
'nponto' is used uninitialized in this function [-Wuninitialized]|
In function 'main':
'y' is used uninitialized in this function [-Wuninitialized]|
'x' is used uninitialized in this function [-Wuninitialized]|

The questions I have are: The way I created the function is correct? What is necessary to remove these 3 warnings when compiling?

1 answer

1


First in your main you created the variables x and y, but did not initiate any value in them.

And in function ImprimePonto the same thing with the variable nponto.

main:

int main(){
    /*...*/
    int x=1,y=3;
    /*...*/
}

ImprimePonto:

void ImprimePonto(Ponto P[]){
    /*...*/
    int i, nponto=0;
    /*...*/
}

When you do not start a value on the variables, they may contain garbage, so when they are passed as values, the results do not appear as expected.

  • Putting an initial value for them, like 0, does not influence the operation of the program so?

  • 1

    @Renan, not putting an initial value causes it to possibly start with random values/memory junk

  • as you are using static values, it is best that you start the values corresponding to the ones you will use

Browser other questions tagged

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