0

this how the variable is passed from the main func:

Node merged = NULL; 
ErrorCode result = mergeSortedLists(left, right, &merged);

and this is the signatue of the func.

ErrorCode mergeSortedLists(Node list1, Node list2, Node *merged_out); 

If an error occurs in func. mergedsortedlists we should return res=Error and the merged list should be NULL

the tutor said we should assign merged_out=NULL; but isn't the address passed by value to the func.? shouldn't we assign the var it points to, to NULL i.e. *merged_out=NULL;

Thanks in advance!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
adlin at
  • 3
  • 2

3 Answers3

1

Everything is passed by value in C.

So yes, &merged (a pointer) is passed by value into mergeSortedLists. In other words, the object to which merged_out points can be changed in that function via pointer deferencing, but any changes in the pointer value in that function will not be reflected in the caller.

I'm not sure why your tutor wants you to write *merged_out = NULL in the function itself. For that to make any sense, then merged_out would have to be a pointer to a pointer, with appropriate changes made at the calling site.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
-1

It seems that the name Node is an alias for a pointer type.

Something like

typedef struct node
{
    //...
} *Node;

So in this code snippet

Node merged = NULL; 
ErrorCode result = mergeSortedLists(left, right, &merged);

the variable merged is a pointer. And it is passed by reference to the function mergeSortedLists.

It is just a bad idea to introduce such an alias for a pointer type because it can only confuse readers of the code.

If to accept that the name Node is an alias for a pointer type (as for example

typedef struct node *Node;

then this function declaration

ErrorCode mergeSortedLists(Node list1, Node list2, Node *merged_out); 

is equivalent to

ErrorCode mergeSortedLists( struct node  *list1, struct node *list2, struct node **merged_out); 

That is all three parameters have pointer types.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    _"passed by reference"_: this term shouldn't be used, there is no passing by reference in C. – Jabberwocky Apr 28 '20 at 12:37
  • I know C, don't worry. It's a C++ term. Read [this](https://stackoverflow.com/a/30519731/898348) as well as other answers to that question. – Jabberwocky Apr 28 '20 at 12:44
  • 1
    @Jabberwocky You know neither C nor C++. Passing by reference is a general term that means that an object is passed indirectly by means of using some mechanism. In each language the implementation of the mechanism can be different. In C the term passing by reference means passing an object indirectly through a pointer to it. – Vlad from Moscow Apr 28 '20 at 12:57
  • @Jabberwocky Even for example in C# the term has a two fold meaning. You can pass an object by reference and you can pass a reference by reference using the specifier ref that for example is absent in Java. – Vlad from Moscow Apr 28 '20 at 13:03
  • 1
    @Jabberwocky: The C standard talks about references and dereferencing. The fact that C++ redefined “reference” to be a particular feature in the C++ language does not abrogate prior meanings of that word in other contexts. – Eric Postpischil Apr 28 '20 at 13:20
-3

Node merged = NULL; merged is pointer to user defined struct. (its done by typedef operator) for example:

struct S_Node{
    int m_var;
    struct S_Node* p_m_next;
};
typedef struct S_Node* Node;  /*now Node is new type. its pointer to S_Node*/

so merged_out is pointer to pointer. so in case of some error in function, we set it to NULL in this code snippet:

ErrorCode mergeSortedLists(Node list1, Node list2, Node *merged_out)
{
    if(!list1 || !list2) /*lets assume that this is the error case*/
    {
        *merged_out=NULL;
        return Error;
    }
}
Adam
  • 2,820
  • 1
  • 13
  • 33
  • 2
    Re “ This statement has no effect in C. because pointer is just like any other variable in c (passed by value), and assign it in you function (mergeSortedLists) will assign a copy of it. so in order to assign it, you need the address of the pointer,…”: The code quoted before this does assign via a pointer; it is `*merged_out=NULL;`, not `merged_out=NULL;`. – Eric Postpischil Apr 28 '20 at 13:14
  • @EricPostpischil, I fixed my answer. i missed some details. thanks. – Adam Apr 28 '20 at 17:50
  • OT: it is a very poor programming practice to hide a pointer via a `typedef`. – user3629249 Apr 29 '20 at 19:10
  • @user3629249 - absolutely! The asker need to ask his instructor why he did so! this is a very bad habit – Adam Apr 30 '20 at 07:26