Chois
Chois

Reputation: 75

Which one is better, 'double pointer' or 'returning pointer' in C?

Belows are image of STACK that i made to show two methods of assignment. Left one, where return 'indexIndicator' from Find() to main() after procedure of Find() is over. Light one is pointer of pointer(double pointer). I know that both work well, but i want to know which one is preferred and why. Thanks enter image description here

Upvotes: 2

Views: 195

Answers (1)

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36391

Well in general, if your function calculates some value you should then use a return; and if your function need to modifies something, you should pass it as a parameter. This rule makes the code easier to read and understand. It is not always easy to follow such a rule, especially when you need to return a bunch of values... Anyway, the semantic of your function should help you.

  • A find() function generally return the value found, pElement = find(list,criterion); for example.
  • A set() function generally takes as parameter the value to be modified, set(&element);

Upvotes: 2

Related Questions