-5

I have these two functions:

void MakeNull_List(List L){
      L->Last=0;
}

void Empty_List(List L){
      return L.Last==0;
}

So, can anyone explain this code for me? What is the difference between L->Last and L.Last?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
toan huynh
  • 33
  • 1
  • 8
  • 2
    You have a function with `void` return type `return L.Last==0;`? Where is this code coming from? – amit Sep 28 '15 at 13:59
  • 3
    A single `=` assigns a value, while `==` tests for equality. You might want to read [a book about C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn more about this. – Bo Persson Sep 28 '15 at 14:02
  • this code is coming from my document's lecturer. and i don't understand this – toan huynh Sep 28 '15 at 14:05
  • 3
    Just pick a good C++ book it should help you to understand this kind of basic concepts. – rkachach Sep 28 '15 at 14:06
  • If that's your lecturer's code, you most definitely want to follow the suggestion from @redobot – bku_drytt Sep 28 '15 at 14:09
  • 1
    At least one of `L->Last` and `L.Last` is wrong, but it's impossible to tell which without seeing the definition of `List`. – molbdnilo Sep 28 '15 at 14:27
  • (`->` and/or `.` might be overloaded for `List`. _amit_ is spot-on pointing out the incongruity of `void` and `return`.) The sole purpose of this snippet may be to stress the indispensability of in-line comments: _code will get separated from external documentation_. – greybeard Sep 28 '15 at 17:41

2 Answers2

0

I hope that this code does NOT come from your lecturer, otherwise you should look for another one...

Sadly this code doesn't make sense because it is wrong! But I think you meant the following:

void MakeNull_List(List *L){    //* added
    L->Last=0;
}

bool Empty_List(List L){        //changed to bool
    return (L.Last==0);         //Please also use brackets here
}

The first function sets the last element to 0 (NULL) whathever sense it should make?!? The second function checks whether the last element is 0 (NULL) or not.

And your actual question: -> is used to Access properties of a pointer to an object and . accesses the properties directly of the object. For more info read here

Community
  • 1
  • 1
Thomas Sparber
  • 2,827
  • 2
  • 18
  • 34
0

Then you access object by variable or reference you should address to it fields by .. Then you have a pointer to object you should previously dereference it. So, you can write (*ptr).someField or ptr->someField.

I think, you miss * in void MakeNull_List(List *L) definition.

Semyon Burov
  • 1,090
  • 1
  • 9
  • 15