In C -> Multiple Struct Passages as Parameter

Asked

Viewed 66 times

0

Good to all!

I am studying about double chained lists, arose the need to pass one struct into another struct, into another.

I already managed to make one, however, instead of using a book struct, I used only an int id inside the node struct.

What I need is to make the correct passage from the struct book, into the struct, which by its made this within the struct list;



With int book_id inside the node itself;

typedef struct no
 {
    in livro_id;
    struct livro *livro;

  }no;

Now I have a separate book struct from the node:

typedef struct livro
{
int id;
}livro;

typedef struct no
{

    struct livro *livro;

}no;

typedef struct lista
{
  no *primeiro;

  }lista;

What is the error of this code?

Obs: remembering that it is just generic code for implementation:

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

typedef struct livro
{
int id;


}livro;

typedef struct no
{

     struct livro *livro;

}no;

typedef struct lista
{
no *primeiro;

}lista;

void iniciarLista(lista *lista)
{
lista->primeiro = NULL;
}

void inserir(lista *lista, livro *livro)
{
no *new = (no*)malloc(sizeof(no));
new->livro = livro;
lista->primeiro = new;

 } 

 void imprime(lista *lista)
 {
 printf("%d", lista->primeiro->livro->id);

 }


 int main()
 {
  lista *lista;
  livro livro;
  livro.id= 10;

  iniciarLista(lista);
  inserir(lista, &livro);
  imprime(lista);


  }
  • Note that its structure is not composed of only a pointer to a book structure. I think she should have given(s) and another pointer to a structure in the.

  • I don’t understand, why should the node structure give data? why another pointer to another node? could you give an example? recalling that it is a Generica implementation, in this case I am not using the implementation of chained lists, I just want to pass the book structure, inside the struct no, which in turn is in struct list. I consent as I said, put any dice within the structure in and pass to the list, but I want to pass any dice, pass another struct more structured.

  • If it is a chained list then each node has a die and a pointer to the next element in the list. If it is a double chained list then each node will have a pointer to the next and another to the previous one.

  • Yes I know, I’ve implemented double-chained list. But when I elaborate my question, if you see the last code, you will see that it is a Generic implementation, not for chained lists, but for the passage from one structure to another struc, which goes to another.

  • My question, and where is the error, what is the reason for not printing on the screen, I am making the book structure, into a no, which put its turn inside a list, not error, but not printa on the screen, I believe that the passage of the pointers is wrong, but I did not find out why

No answers

Browser other questions tagged

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