I'm pretty new with C so I have encountered many doubts with pointers. I've already search a lot about this but there are some things that still are not clear for me, and I also think this will help other beginners:
I'm currently working with something like a binary search tree, more specifically a dictionary. In both cases I need a pointer to the left subtree an another one to the right. In the dictionary also a key and a value.
Lets supose we have a struct node with key,value,left and right(just a summary idea): Where left and right are pointers to other node
dictionary new_dict = calloc(1,sizeof(struct node)); new_dict->key=..; new_dict->value=..; // some values new_dict->left=..; new_dict->right=.. ;
I'm doing pretty well so far but in some cases I find myself in a situation like this:
dictionary->left->key
Or maybe more: ptr->ptr->ptr->ptr (any number of times)
Does this represent a problem?
Does this affect the performance?
Is it a bad practice?
Should I try avoiding this?
dictionary->left->key
is OK, but if you're finding yourself sayingptr->ptr->ptr->ptr
a lot, read The Law of Demeter is not a Dot Counting Exercise.