Thread: Linked List, pointers, function question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    Linked List, pointers, function question

    Code:
    struct primeList {
           int prime;
           struct primeList *next;
    };
    int main(int argc, char *argv[]) {
            time_t start,end;
            time (&start);
            int lastTry = atoi(argv[1]);
    	struct primeList *firstPrime = new struct primeList;
    	firstPrime->next = NULL;
    	struct primeList *latestPrime = firstPrime;
    	struct primeList *thisPrime = firstPrime;
    	ifstream fin("fpf2.log");
            while (fin) {
                 thisPrime = latestPrime;
                 latestPrime->next = new struct primeList;
                 latestPrime = latestPrime->next;
                 latestPrime->next = NULL;
                 fin >> latestPrime->prime;
           }
           latestPrime = thisPrime;
           latestPrime->next = NULL;
           delete latestPrime->next;
           fin.close();
    .........
    .........
    Please help me put the last 12 lines of code shown into its own function. Pointers confuse me. I've tried various things but in reality I've no clue what I'm doing. I want to do this for neatness's sake and for learning purposes. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Put the last 14 lines into a function. Then you only need to pass firstprime as an argument, and everything should just work.

    BTW, some of that code is not quite correct. The most obvious example: you are setting a pointer to NULL and then deleting it, which does nothing.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Quote Originally Posted by Daved
    Put the last 14 lines into a function. Then you only need to pass firstprime as an argument, and everything should just work.

    BTW, some of that code is not quite correct. The most obvious example: you are setting a pointer to NULL and then deleting it, which does nothing.
    Good catch, thanks.
    But I want to be able to use latestPrime and thisPrime (thisPrime for neatness's sake) later in main().
    Basically, how can you make a function f that can access pointer x, y and do anything it wants to them.
    Very elementary, I know, but that's why I need to know this.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Pass the latestPrime and thisPrime pointers by reference so that when they are changed, the variables in main are updated with those changes.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Pointers:

    All variable names and their corresponding values are stored in memory somewhere. The location in memory where they each reside is denoted with an address. An address is somewhat scary looking:

    006BFDF4

    but you can just think of it as a mailbox number. If you look inside the mailbox with that address, you'll find the variable name and its corresponding value. For instance, if you declare an int:

    int num = 10;

    that line stores the variable name "num" with a value of 10 in a mailbox somewhere. We don't know where the mailbox is located yet. However, there is an "address of" operator which will retrieve the location of the mailbox, e.g.

    cout<<&num<<endl;

    You can store that address in a special variable called a pointer:

    int* p;

    That declares a variable named p, and p is a variable of type "pointer to int". Essentially, you read the line in reverse. After declaring p, you can assign the address of any int to p:

    p = &num;

    which reads: "p equals the address of num".

    Finally, you can take a pointer and get the value stored in the mailbox at that address by putting a little star in front of the pointer name:

    cout<<*p<<endl;

    -------------------------

    First, instead of writing your pointers like this:

    struct primeList *firstPrime

    I suggest you write your pointers like this:

    struct primeList* firstPrime

    It makes it clear the variable name is firstPrime and the type is:[edited] primeList*. So, firstPrime points to a mailbox(place in memory) that contains a primeList struct.

    Basically, how can you make a function f that can access pointer x, y and do anything it wants to them.
    Very elementary, I know, but that's why I need to know this.
    All functions declare the type of their parameters and those types must match the type of the arguments you plan on sending to the function. When you pass a pointer to a function, you can change what it points to--to your heart's content, e.g.:

    *p = 150;

    which means num will equal 150 in the example above.
    Last edited by 7stud; 11-01-2005 at 10:32 PM.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Doesn't it get more complicated when -> gets involved?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The '->' symbol is just shorthand to make things easier to type. It is equivalent to:

    (*pointerToObject).member

    Instead of having to type that, you can do this:

    pointerToObject->member

    That says dereference my pointer to an object, which gives me the object, then access the specified member using the member access operator '.'

    However, the first method will work just as well if you like typing.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    You people humble me with your willingness to help newbs.

    Code:
    struct primeList {
           int prime;
           struct primeList *next;
    };
    int importList(struct primeList *latestPrime, struct primeList *thisPrime) {
        ifstream fin("fpf2.log");
           while (fin) {
                 thisPrime = latestPrime;
                 latestPrime->next = new struct primeList;
                 latestPrime = latestPrime->next;
                 latestPrime->next = NULL;
                 fin >> latestPrime->prime;
           }
        latestPrime = thisPrime;
        delete latestPrime->next;
        latestPrime->next = NULL;
        fin.close();
    }
    int main(int argc, char *argv[]) {
        time_t start,end;
        time (&start);
    	int lastTry = atoi(argv[1]);
    	struct primeList *firstPrime = new struct primeList;
    	firstPrime->next = NULL;
    	struct primeList *latestPrime = firstPrime;
    	struct primeList *thisPrime = firstPrime;
    	importList(latestPrime, thisPrime);
    .......
    .......
    Unfortunately, this doesn't work. I'm not sure what in particular doesn't work, but it doesn't do that same thing as the code in my first post. It compiles without errors in Dev-C++ (I don't see why it wouldn't). Sorry for the lousy formatting; I don't know how to set up Dev-C++ to not auto tab.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Sorry, I made a mistake up there somewhere. Structs create a new type like int, double, string. The name you give to the struct is the name of the type. So the type of a primeList struct is primeList. The type of a pointer to a primeList struct is primeList*.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You didn't pass the pointers by reference, so when the function changes the value of the pointers, the code in main (the ........ part) doesn't use the new pointer values. I don't know if that is your problem, but since you said you use latestPrime and thisPrime later in main(), if you don't pass them by reference that code won't have the correct values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM