-2

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?

3
  • 1
    The answer is "no" to all four of your questions. Although to be fair, dictionary->left->key is OK, but if you're finding yourself saying ptr->ptr->ptr->ptr a lot, read The Law of Demeter is not a Dot Counting Exercise.CommentedMay 21, 2020 at 4:54
  • You are far too early in the learning process to be told rules. Rules are too often applied blindly. I have seen too much absolutely stupid code that came from blind adherence to rules without understanding them.CommentedMay 21, 2020 at 12:25
  • Does this affect performance? To check performance, you use a profiler to measure it.CommentedMay 21, 2020 at 12:26

2 Answers 2

4

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?

Yes and No. In itself, it is not a problem if you use the -> operator multiple times in an expression to reach the value you want to have.

However, the more arrows you need to follow, the harder it becomes to understand where you are in your data structure and what your code is doing. And not understanding your code is a problem.

Does this affect the performance? Is it a bad practice?

No to both of them.

Should I try avoiding this?

No, but you should try to avoid writing code that is hard to understand.

If you find yourself writing a block of code where you often use dictionary->left->something, see if byou can move that block of code into a function of its own where you can pass the pointer dictionary->left as an argument. The same function might even be useful to be called with dictionary->right.

2
  • +1 just for "avoid writing code that is hard to understand"
    – Caleth
    CommentedMay 21, 2020 at 10:00
  • Also: If you’re told to avoid something, don’t replace it with something ten times worse.CommentedMay 21, 2020 at 12:34
3

You use it to refer to a struct POINTER member. Take this struct:

struct MyStruct { int x; int y; float z; }; 

Then you create a variable:

struct MyStruct AStruct; 

You would refer to the members like this:

printf("x: %d\n", AStruct.x); 

But if you have a pointer:

struct MyStruct *PointerToAStruct = &AStruct; 

You would use -> to reference the members like this:

printf("x: %d\n", PointerToAStruct->x); 

So basically, PointerToAStruct->x is just shorthand for (*PointerToAStruct).x like how @candied_orange mentioned in a comment.

To answer your other questions, having something like dictionary->left->key is just a pointer to a struct with a pointer to another struct with a member. There isn't anything wrong with that.

1
  • 2
    There isn’t anything wrong with a->b->c->d ... as long as all pointers are valid (e.g. not null). Unfortunately, when there are long chains, people tend to be too optimistic, because when in shortcut mode, writing if (a&&a->b&&a->b->c) a->b->c->d...; might appear as a torture ;-)CommentedMay 21, 2020 at 9:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.