[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What does this mean in C?

Posted on 2011-10-22
11
Medium Priority
?
260 Views
Last Modified: 2012-05-12
return L->Next == NULL;

Most specifically what does
->

Open in new window

do

I expect this is extremely basic
0
Comment
Question by:GPicasso
9 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 100 total points
ID: 37011701

In C in this case L is a pointer to the structure, and the structure has element (or maybe they call ift filed or member in C - don't rememeber) this filed is called Next

  == makes it boolean test, so my undrstanding is that
if filed Next in instance L of certain structure is NULL then it will return true; if it is not NULL it will return false.

0
 
LVL 31

Expert Comment

by:farzanj
ID: 37011839
-> is the equivalent of (*L).Next.  

In C, a variable (x) of a struct (A) is accessed by A.x

Now if you have a pointer (B) to the struct, you have to use (*B).x or B->x
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37011865
As for_yan said, L is a pointer to a structure.  You would find out which structure by finding the declaration for L.  In this case, I would guess that this function is processing a linked list (or similar).  Here's an example (untested).

typedef Node
{
    Node *link;
    char item [ 50 ];
} Node;

int isLastElement ( struct Node *L )
{
    return L->next == NULL;
}

main()
{
   ...
   if ( isLastElement ( &Node ) )
        printf ( "We are at the last element\n" );
    ....
}

Open in new window


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37011867
farzanj just said what I was going to put in my next post.  He is correct as well.
0
 

Author Comment

by:GPicasso
ID: 37012309
Ok so like if I have a struct named L

->L is like *L  
meaning it returns the address of L... correct?
0
 

Expert Comment

by:sonagu
ID: 37012371
Next should be another field of the structure that give the address of the next estructure data, this type of definition is used to create a linked list.  *L = address of the current node of the structure
L->next = address of the next node of the structure

check the following
http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_086.htm 
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37012562
Evilrix, I don't know where the repeat comes from.  If it's me, it's one of my tutoring methods that's been successful in getting to the "Oh....I see" moment.  It doesn't work for everyone.  It works for some students.

Picasso, L-> is like (*L).  Please look at farzanj's post again.  Then look at my first post with code.  Replace line 9 with

    return (*L).next == NULL;

Open in new window


Mostly L-> is easier to type and read.

sonagu's reference is very good if you need/want help understanding Linked Lists.
0
 
LVL 12

Assisted Solution

by:satsumo
satsumo earned 100 total points
ID: 37012758
->L is like *L  
meaning it returns the address of L... correct?]
No, ->L doesn't work.  '->' is for structure members, not pointers.  In this case L is a pointer, it's already an address.

L is a pointer to a structure.
*L is the value of L, the contents of the memory pointed to.
L->next is the value of the 'next' member of the structure.

Another example:
typedef struct
{
    int a;
    int b;
}
THING;

void main (void)
{
    int answer;
    THING fred;
    THING *ptrFred;

    fred.a = 5;
    fred.b = 3;

    ptrFred = &fred;  // ptrFred points to fred, it's value is the address of fred
    
    answer = ptrFred->a; // 'answer' will be 5
    answer = answer * ptrFred->b; // 'answer' is now 15
}

Open in new window


Apologies if I'm repeating something that has already been said.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 37012962
Evilrix, my apology if I repeated/re-worded the previous post, although I never though it was against EE policy and if it is please quote it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question