Pointer

Asked

Viewed 52 times

0

Good afternoon, I am trying to solve an exercise of a college subject, however I am not able to correctly manipulate the use of pointers, follows down the statement and the code.

We have seen that altering the RA of a1 does not alter the RA of the student in c.

This happens because c does not have a1 in its student list. Class c has a copy of a1 in your list.

Suppose we want the change in a1 to cause a change in c. For this, the list of students in c, should not contain students, but pointers for students.

Change student field *students in c to student **students. Now students represents a list for *student.

Modify the rest of the code so that the change in a1 causes change in c.

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

typedef struct aluno_s {
  int RA; 
  int CPF;
} aluno;

typedef struct classe_s {
  int n; //quantidade de alunos
  aluno *alunos;
} classe;

void imprime(classe c) {
  for(int i=0; i<c.n; i++) {
    printf("%d %d\n", c.alunos[i].RA, c.alunos[i].CPF);
  }
}
void adiciona(aluno a, classe *c) {
  c->alunos[c->n] = a;
  c->n++;
}

int main(void) {
  aluno a1 = {123, 456};
  aluno a2 = {789, 101};
  classe c = {0};
  c.alunos = malloc(sizeof(aluno)*100);

  adiciona(a1, &c);
  adiciona(a2, &c);
  imprime(c);

  a1.RA = 999;

  imprime(c);
  return 0;
}

Satida

123 456
789 101
123 456
789 101

In the army I must change the line of code

aluno *alunos

for

aluno **alunos

and after that make the necessary changes to make the code work, however when I do this and adjust the code it is returning me the memory addresses. Below is my changed code.

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

typedef struct aluno_s {
  int RA; 
  int CPF;
} aluno;

typedef struct classe_s {
  int n; //quantidade de alunos
  aluno **alunos;
} classe;

void imprime(classe *c) {
  for(int i=0; i<c->n; i++) {
    printf("%d %d\n", c->alunos[i]->RA, c->alunos[i]->CPF);
  }
}
void adiciona(aluno a, classe *c) {
  c->alunos[c->n] = &a;
  c->n++;
}

int main(void) {
  aluno a1 = {123, 456};
  aluno a2 = {789, 101};
  classe c = {0};
  c.alunos = malloc(sizeof(aluno)*100);

  adiciona(a1, &c);
  adiciona(a2, &c);
  imprime(&c);

  a1.RA = 999;

  imprime(&c);
  return 0;
}

Exit

-1761299816 32765
-1761299816 32765
-1761299816 32765
-1761299816 32765

Out that I want

123 456
789 101
999 456
789 101

1 answer

4


You are copying the student,

void adiciona(aluno a, classe *c) 
{
    c->alunos[c->n] = &a;
    c->n++;
}

so a copy of it is added to alunos but when leaving the scope of the function adiciona that copy dies and alunos points to a memory junk.

just pass a reference or pointer of the student who is in the main

void adiciona(aluno *a, classe *c) 
{
    c->alunos[c->n] = a;
    c->n++;
}
  • 1

    Thank you very much Penachia, I have a certain difficulty to understand pointers, but your reply was very clear and direct. PS: It worked!!!

Browser other questions tagged

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