How to reference two structures in C properly?

How to reference two structures in C properly?



I have a structure defined as:


typedef struct
int pages;
book;



I first declare an instance:


book *b=malloc(sizeof(book));
b->pages=35;



Then I declare another book pointer as:


book *a;
a=&b;



After debugging I see that the member in structure a doesn't resemble b? I get false value for pages in a.


a


b


pages


a



Isn't supposed that a points to the memory of b? How can I dereference a properly? Sorry if I have not understood the concept of pointers right.


a


b


a





What's the type of &b? Did you compile with -Wall -Wextra -pedantic and get a warning free build?
– StoryTeller
Aug 27 at 8:22


&b


-Wall -Wextra -pedantic





a type is pointer to book. &b type is pointer to pointer to book. You probably intended to do a = b here.
– Mindaugas
Aug 27 at 8:26


a


pointer to book


&b


pointer to pointer to book


a = b





a=&b; isn't valid C code and will not compile, so that would be why.
– Lundin
Aug 27 at 8:34


a=&b;





@user3597222: A pointer variable does not contain the structure data; it only contains a "reference" to where the data is. The malloc(sizeof (book)) allocates memory for the structure data, and returns the pointer (the "reference") to it. You can assign that value to as many pointers as you wish, and they all will refer to the same book data, equally. The -> operator tells the compiler that the left side is a pointer to a structure, and the right side names the member the expression should evaluate to. Compare to ., where the left side refers to the structure, not a pointer to one.
– Nominal Animal
Aug 27 at 10:48



malloc(sizeof (book))


->


.




4 Answers
4



What you do here is storing the address of b in a. When dereferencing a now you get what is stored in b which is the address of where your book is stored, not the book itself. In fact I think you should get a segmentation fault when trying to access a->pages because this memory wasn't even allocated.


b


a


a


b


a->pages



So I think what you want to do here is to make a a pointer to the same book structure as b which you can achieve by just doing


a


book


b


book *a = b;



In order to do this you need to make a as pointer to pointer because b is pointer.


a


b


book **a;
a=&b;



you can do as below with a using just a pointer.


a


book *a;
a=b;





There is a difference in between "double pointer" and "pointer to pointer".
– haccks
Aug 27 at 8:25



double





Is it ok that after doing what you wrote, when I change the value of pages at a, I get it also in b?
– user3597222
Aug 27 at 8:37





Yes, that's what for pointer is.
– kiran Biradar
Aug 27 at 8:46



What you are doing with this line:


a=&b;



is to assign a pointer to book to a pointer to pointer to book.


book


book



Usually, the compiler will emit an error which will look something like this:


error: cannot convert 'book**' to 'book*' in assignment.



But it looks like you have directly run it in debug mode.
So it is good to compile the code, remove all the errors and warnings and then start debugging.



First of all you have to know a pointer is a variable, it is also have the address, you define a pointer variable, if you don't give it to apply for space, the system will automatically give it a space, so, if you want to let two Pointers equal you'll have to define a secondary pointer, then the secondary pointer to the address is another pointer itself.So you should change it like this


book **a; //a is a second level pointer
a=&b; //Now a is pointing to the address of b itself,Because b has an address






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)