Warnings when compiling the program

0

In the code below, I need to create a structure for Point (Coordinate x and y) and create a function that creates those 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 points and another function to print these points:

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 function (main) 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: Is the way I created the function correct? What do I need to remove these 3 warnings when compiling?

    
asked by anonymous 30.05.2017 / 18:40

1 answer

1

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

And in the 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 in the variables, they may contain garbage, so when they are passed as values, the results do not appear as expected.

    
30.05.2017 / 20:10