how do I access the values of a struct?

0

I need to print the read values on the keyboard of a struct vector that form a coordinate. The problem is that when I pass the struct vector of the function that picks up and stores the data for function that will print them on the screen, the outputs are coordinates (0,0) regardless of what is typed. I tried to pass by reference and value and did not work both ways.

It is probably some error of passage itself, but I can not find nowhere the correct way to do ...

struct armazenar
{
    float x;
    float y;
};


int func_lerCoordenada(int n)
{
    struct armazenar p[MAX];
    int i;

    for(i=0;i<n;i++)
    {
        puts("Insira a coordenada x:");
        p[i].x=lerfloat();//lerfloat só valida enrada pra float

        puts("Insira a coordenada y:");
        p[i].y=lerfloat();

        printf("%f,%f\n",p[i].x,p[i].y);//só verificando que os valores lidos estão sendo armazenados corretamente, portanto o problema não é aqui.

    }
    return 0;
}


float imprimePonto(struct armazenar p[], int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("(%9.2f, %9.2f)\n",p[i].x,p[i].y);
    }
    return 0;
}


float comparacao(struct armazenar p[],int n)
{
    int i, pac=0,pab=0,pes=0,pdi=0;
    float dir=0,esq,acima=0,abaixo=0;

    for(i=0;i<n;i++)
    {
        if(p[i].y>acima)
        {
            acima=p[i].y;
            pac=i;//para registrar a posicao em que a condicao era verdadeira
        }
        else
        if(p[i].y<abaixo)
        {
            abaixo=p[i].y;
            pab=i;
        }

        if(p[i].x>dir)
        {
            dir=p[i].x;
            pdi=i;
        }
        else
        if(p[i].x<esq)
        {
            esq=p[i].x;
            pes=i;
        }

    }
    printf("O ponto mais acima eh: (%9.2f,%9.2f).\n\n",p[pac].x,p[pac].y);
    printf("O ponto mais abaixo eh: (%9.2f,%9.2f).\n\n",p[pab].x,p[pab].y);
    printf("O ponto mais a esquerda eh: (%9.2f,%9.2f).\n\n",p[pes].x,p[pes].y);
    printf("O ponto mais a direita eh: (%9.2f,%9.2f).\n\n",p[pdi].x,p[pdi].y);
}


int main()
{
    struct armazenar p[MAX];
    int n=0;

    n=func_lerN();//recebendo o numero de vetores que o usuario quer digitar.

    func_lerCoordenada(n);
    imprimePonto(&p[MAX],n);
    comparacao(&p[MAX],n);


    return 0;
}
    
asked by anonymous 08.12.2016 / 00:57

2 answers

3

Your problem is in main , when you declare your vector struct armazenar p[MAX] a sequence of values is created.

    0         1       2        3        4      N
__________________________________________________
|armazena|armazena|armazena|armazena|armazena|...|
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Note that in main , you create your vector, but you do not enter values in it. So whenever you get to the imprimePonto method it will show 0

The resolution is to change the func_lerCoordenada method by getting it to receive its vector as a parameter.

/* usar *p ou p[] é a mesma coisa quando usado dentro do método*/
int func_lerCoordenada(struct armazenar *p, int n){
    int i;

    for(i=0;i<n;i++){
        puts("Insira a coordenada x:");
        p[i].x=lerfloat();//lerfloat só valida enrada pra float

        puts("Insira a coordenada y:");
        p[i].y=lerfloat();

        printf("%f,%f\n",p[i].x,p[i].y);//só verificando que os valores lidos estão sendo armazenados corretamente, portanto o problema não é aqui.

    }
    return 0;
}

Every vector in C / C ++ is a memory address that stores a predefined sequence of values, unlike a Pointer to be dynamically allocated and not to change its size.

And at the time of printing the data you are passing &p[MAX] or you are passing the address of the last + 1 position of the vector, thus there will be no information to be shown on the screen.

As your method traverses the vector from the beginning of it, you must only pass the vector without any position.

int main(){
    struct armazenar p[MAX];
    int n=0;

    n=func_lerN();

    func_lerCoordenada(p, n);
    imprimePonto(p, n);
    comparacao(p, n);


    return 0;
}
    
08.12.2016 / 03:24
0

I made this example, to get an idea.

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

typedef struct Ponto {

    int x;
    int y;
}ponto;

ponto* atribuir(ponto **p) {

    printf("Digite x: ");
    scanf("%d", &(*p)->x);
    printf("Digite y: ");
    scanf("%d", &(*p)->y);
    return *p;
}

void exibir(ponto *p) {
    printf("\nExibindo Pontos.\n");
    printf("ponto x: %d\n", p->x);
    printf("ponto y: %d\n", p->y);
}
int main()
{
    ponto *p;
    p = malloc(sizeof(ponto));
    p = atribuir(&p);
    exibir(p);
    free(p);
    return 0;
}
    
08.12.2016 / 01:27