Thread: linked lists .

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    linked lists .

    PLZ help !

    the file attached .

    i dont understand the idea of this function

    Code:
    void reg_student(slist *students,clist *courses,int id, int number)
    this function is VOID , how can add course to student by void function ;

    how to change student->course ?

    supose student->course = A->B->C->NULL ;

    can i had a newCourse ?

    after using
    Code:
    void reg_student(slist *students,clist *courses,int id, int number)
    student->course = newCourse ->A->B->C->NULL ;

  2. #2
    Registered User
    Join Date
    Dec 2009
    Posts
    12
    יפה, בדיוק אותה שאלה לשנינו
    מי זה??

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    English please.

    All functions take values. The first two values this function takes are memory addresses to structures. Those structures will also have pointer. So, let's assume it's like so:
    Code:
    struct foo
    {
        struct foo *next;
        ...data...
    };
    Then we make an instance of that structure, and set its pointer to point to NULL.
    Code:
    struct foo *instance = malloc( sizeof *instance );
    instance->next = NULL;
    Now, if I pass the address of 'instance' to a function, I can access 'instance->foo' inside that function, because, through the address, I can access all of its members. Since I can access that member, I can make that member's value change--and in this case, I can make it point to a new structure.
    Code:
    void bar( struct foo *p )
    {
        if( p )
        {
            struct foo *q = malloc( sizeof *q );
            if( q )
            {
                q->next = NULL;
                p->next = q;
            }
        }
    }
    Assuming of course, that we know the structure we are passing has a NULL for its 'next' member. Otherwise you'd naturally want to tweak this a bit to handle if it were not null all ready.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    47
    Code:
    void func1( int i ){} //pass by reference  (can't manipulate contents unless i is global
    void func2( int *i ){} //pass by address (manipulate contents of non-local variable
    
    int i;
    
    func1( i );
    func2(&i)
    
    // &i gives the address, pasising a pointer to it in memory.
    if I didn't use int i, and wanted to use int *i to innitialize it, I would be responsible for allocating memory sizeof(int) for it to hold a value.

    "->" is just that, a pointer to a structure's member in memory, if you have multiple instances of a struct you must always use ->, otherwise you can use a "." to access its members.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just a few minor corrections with respect to tempster09's post #4:

    Code:
    void func1( int i ){} //pass by reference  (can't manipulate contents unless i is global
    The above actually demonstrates pass by value, not pass by reference.

    Code:
    void func2( int *i ){} //pass by address (manipulate contents of non-local variable
    It is true that an address is passed in the above function, and although the pointer is passed by value, this simulates pass by reference.

    Quote Originally Posted by tempster09
    "->" is just that, a pointer to a structure's member in memory, if you have multiple instances of a struct you must always use ->, otherwise you can use a "." to access its members.
    I think tempster09 meant that -> is a form of pointer notation, and indeed a->b is syntactic sugar for (*a).b, where a is a pointer to a structure which has a member named b. The part about "multiple instances of a struct" sounds like a mistake: I think "a pointer to a struct" is the intended wording (although the "must always" clause is obviously not true).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM