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
Asked
Active
Viewed 184 times
2

Chois
- 75
- 1
- 2
- 7
-
3Please use code to describe code, not hard-to-understand images. – unwind Jan 20 '15 at 11:18
-
The images are a nice touch though. – Bennie Jan 20 '15 at 11:28
-
Lemme get this straight. You're asking which of `void find(void* key, void** val)` and `void* find(void* key)` is preferable? The answer is "it depends". I personally favour `int find(void* key, void** val)`, since then `find` can be used both as an _is-present_ test (By passing a `NULL` as the second argument) and as an actual _find_, plus it's consistent with a `int del(void* key, void** delVal)`, where I _don't_ want the user to be able to simply chuck away dynamically-allocated memory. On the other hand the 2nd variant lets you do stuff like `find(key)->pointer.madness`. – Iwillnotexist Idonotexist Jan 20 '15 at 11:47
-
Please provide some code, I need to understand, on the left will indexes be a pointer to the first pointer in Heap or will indexes point to the same address as the first element in heap points to? – Bennie Jan 20 '15 at 11:48
-
irrelevant to your question bu how did you generate this neat diagram? – barej Jan 20 '15 at 13:07
-
1@barej just power point :) – Chois Jan 20 '15 at 14:07
1 Answers
2
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);

Jean-Baptiste Yunès
- 34,548
- 4
- 48
- 69
-
-
Maybe this topic will help you more ? http://stackoverflow.com/questions/721090/what-is-the-difference-between-a-function-and-a-procedure `find` is a function (in mathematical sense) and `set` a procedure (a pure computer programming concept). A math function is generally implemented as a C-function returning a value, and a procedure is implemented as a C-function that takes pointers as arguments so that it can modify them. – Jean-Baptiste Yunès Jan 20 '15 at 15:22