Problem to link two structs in C

Asked

Viewed 36 times

-1

The program is about a bank, I need two structs (one of client and one of accounts), the account holder has to be linked with some client. But in the Add_account function, I am not able to make this link, I need a way to point to the place where the client is registered, or any suggestion of how I could do. Later I will need to print the clients of an agency, so I would need to filter data from the two structs.


//Declara��o structs - Cliente e Conta
typedef struct
{
  char nome[100];
  int RG;
  char endereco[200];
  int telefone;
  float renda;
  int tipo_cliente; //1-private, 2-alta renda, 3-varejo
  union
  {
      int cpf;
      int cnpj;
  } cpf_cnpj;
  int num_cl;
} Cliente;
Cliente cliente[MAX_CLIENTES];
char nome[100];
char endereco[200];

int posicao;

typedef struct
{
  int Numero_Conta; //deve ser unico e gerado pelo programa
  int Agencia;
  float Saldo_Conta;
  int Tipo_Conta;          // 1- Conta corrente 2-Conta poupanca 3- conta-salario
  struct Cliente *Titular; // titulares (deve estar vinculado aos clientes cadastrados)

} Dados_Contas;
Dados_Contas contas[MAX_CLIENTES];


void Add_Conta(int j, int num)
{
  int id;
  printf("Preencha os dados a seguir: \n \n");
  printf("Agencia: \n");
  scanf("%d%*c", &contas[j].Agencia);
  contas[j].Numero_Conta = num;
  printf("O sistema definiu o seguinte numero para a conta: %d \n", num);
  printf("Saldo da conta: \n");
  scanf("%f%*c", &contas[j].Saldo_Conta);
  printf("Tipo da conta: \n 1- CONTA-CORRENTE \n 2- CONTA POUPANCA \n 3- CONTA-SALARIO \n");
  scanf("%d%*c", &contas[j].Tipo_Conta);
  printf("Qual o numero (identificador) do cliente titular? ");
  scanf("%d", &id);
  // contas[id]->Titular=&cliente[id];1
}

  ```
  • Post a complete program, something compilable. Help others help you... In your Client program is a structure. A name. And so customer is Customers[MAX_CLIENTES]; so declaring Holder as a pointer to a Customer is not a struct. Use only Customer* Holder

  • Next time I put the full code, thank you for your help!

1 answer

0

You are probably suffering at the time of de-referencing an address.

See this excerpt from the code:

contas[j]->Titular = &cliente[id];

With the operator -> you de-reference an address, it says that the code should check the address that is written on the pointer, and continue to access from that address.

The value of contas[id] It’s not an address, he’s the type Dados_Contas, not of the kind Dados_Contas*, which means you shouldn’t be de-referencing it. It should be:

contas[j].Titular = &cliente[id];

Now the attribute Titular is a pointer of the kind struct Cliente*, it records an address, and then it would make sense to reference that address to access one of the properties of that structure, as would be the case:

printf("Nome do cliente: %s \n", contas[id].Titular->nome);
  • That’s right! Thank you very much!

Browser other questions tagged

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