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?
Putting an initial value for them, like 0, does not influence the operation of the program so?
– Renan
@Renan, not putting an initial value causes it to possibly start with random values/memory junk
– Jefferson Quesado
as you are using static values, it is best that you start the values corresponding to the ones you will use
– Brumazzi DB