How do I access the values of a struct?

Asked

Viewed 1,127 times

0

I need to print the values read 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 takes and stores the data to function that will print them on the screen, what comes out are coordinates (0.0) regardless of what is typed. I’ve tried going by reference and value and it didn’t work both ways.

It’s probably some kind of mistake in passing, but I can’t find the right way to do it anywhere...

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;
}

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 the main, you create your vector, but you do not enter values in it. So whenever you arrive at the method imprimePonto will show 0

The resolution is to change the method func_lerCoordenada causing 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, which is different from a Pointer does not need to be dynamically allocated and does not need to change its size.


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

As your method traverses the vector from the beginning of it, you must pass only the vector with no 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;
}

0

I did this example, from 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;
}
  • Unfortunately this answer will be useless for me because I haven’t learned it yet and I don’t understand what has to be done, but thank you.

  • You have to pass the struct in the reading function.

  • or makes the function point to the one of the parameter.

Browser other questions tagged

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