How to print the two vector values in the pointer struct?

Asked

Viewed 75 times

0

I made a code with struct Pessoa to save more than one note and print using pointer. My question: how do I print in function listar() the two notes I recorded in the function main()? I’m doing it right?

I put the two notes in the first index [0] to be able to test the function only.

I want to list the notes of the registered person.


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

struct pessoa{
    int notas[4];
};

void listar(struct pessoa *n1){
    int cont=0;
    for(cont=0;cont<2;cont++){
        printf("\n--Nota:");
        //Duvida aqui:
        printf("%d",(*(n1 + cont)->notas));

    }

}

main(){
    printf("\n Digite a nota:");
    struct pessoa n1[10];
    int i;
    for(i=0;i<2;i++){
        printf("\nDigite a nota:");
        scanf("%d",&n1[0].notas[i]);
    }
    listar(n1); 
}

  • If notes are an array of 4 positions you need to print element by element of the array and not the way you did.

  • And you want to list what? people and their notes or just the notes people registered?

  • I do this with the " * "? I don’t know for sure, I tried to put *(n1)->notes[cont], but I don’t know

  • I want to list the notes of the registered person

2 answers

2

You are making some mistakes there. If you want to list a person pass a person and not all people. Then everything becomes simple and easy. Note that in addition to receiving only one person I am passing one as an argument, the one who just typed. With this code could not even be different.

It may be that I want to do something else, but within the question would be this.

I gave an improved because the code was very confusing and complicated, pay attention to every detail.

#include <stdio.h>

struct pessoa {
    int notas[4];
};

void listar(struct pessoa pessoa) {
    for (int i = 0; i < 4; i++) printf("\n--Nota %d: %d", i + 1, pessoa.notas[i]);
}

int main() {
    struct pessoa n1[10];
    for (int i = 0; i < 4; i++) {
        printf("\nDigite a nota:");
        scanf("%d", &n1[0].notas[i]);
    }
    listar(n1[0]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • You’ve helped me, thank you. If possible, can you help me do the same thing using pointer? Print the same thing, only with pointer in the List function?

  • Not because it doesn’t make sense to use pointer there.

  • Only if I had to list the 10 people along with the grades?

  • Then it would make sense.

1


I understand that you want to pass the struct by reference, avoid redundancies and optimize the code, but the notation *(ptr + pos) is equivalent to ptr[pos] in C... that is, you are trying to access a memory address that has not been designated, If you want to print all of a single student’s notes you must iterate through the notes array, not person

for (int i = 0; i < numNotas; i++) {
    printf("%d ", pessoa.notas[i]);
}

In the form that is receiving a pointer in the function besides being a good practice because it is passing the data by reference gained the possibility to list the notes of n students, I would write it as follows:

void listar(struct pessoa *pessoas, int numPessoas, int numNotas) {
      for (int i = 0; i < numPessoas; i++) {
         printf("Aluno %d:", i+1);
         for (int j = 0; j < numNotas; j++) {
            printf(" %d", pessoas[i].notas[j]);
         }
         printf("\n");
      }
   }
  • To access the contents of a pointer, you do not need the *?

  • In this case not because the notation ptr[i] = *(ptr + i), if 'people' has only one struct you can access it with both *people and people[0]

Browser other questions tagged

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