Pointer pointing incorrectly

This is a discussion on Pointer pointing incorrectly within the C Programming forums, part of the General Programming Boards category; What this program is supposed to do is take in user input and print them in lexicographic order using a ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Pointer pointing incorrectly

    What this program is supposed to do is take in user input and print them in lexicographic order using a linked linked. Say I typed in the words foo, bar, qux; the program should print out bar, foo, qux.
    My problem is that whenever I insert a word, it becomes the value at the head of the node. I may have the pointers pointing in the wrong place, but I have no idea at this point.

    Code:
    #include <assert.h>
    #include <libgen.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef char *cstring;
    typedef struct node *node_ref;
    struct node {
       cstring string;
       node_ref link;
    };
    
    struct list list_ref;
    struct list {
       node_ref head;
    };
    
    void insertAscending( struct list *listref, char *input )
    {
       node_ref prev = NULL;
       node_ref curr = (*listref).head;
       if ( curr != NULL )
          printf("what is listref->head %s\n", (*listref).head->string);
       // Find the insertion position.
       while (curr != NULL) 
       {
          if (strcmp(curr->string, input) > 0) 
          {
          	printf("what is strcmp %d", strcmp(curr->string, input));
             break;
          }
          /*printf("what is curr->string %s\n", curr->string);
          printf("what is input %s\n", input);
          printf("what is strcmp %d\n", strcmp(curr->string, input));*/
          prev = curr;
          curr = curr->link;
       }
    
       // Do the insertion.
       node_ref temp = NULL;
       temp = malloc (sizeof (struct node));
       assert (temp != NULL );
       temp->string = input;
       temp->link = curr;
       
       printf("%s\n", temp->string);
       
       if (prev == NULL) 
       {
          listref->head = temp;
          printf("0\n");
       }
       else
       { 
          prev->link = temp;
          printf("1\n");
       }
    
    };
    
    int main (int argc, char **argv) {
       char *progname = basename (argv[0]);
       char buffer[256];
       int linenr;
       struct list *list_ptr;
       list_ptr = &list_ref;
       
       for (linenr = 1; ; ++linenr) {
          char *nlpos = NULL;
    
          // Read a line of input and check to see if it ends with
          // a newline character.  Print a message if not.
    
          char *gotline = fgets (buffer, sizeof buffer, stdin);
          if (gotline == NULL) break;
    
          nlpos = strchr (buffer, '\n');
          if (nlpos != NULL) {
          
             *nlpos = '\0';
          }else 
          {
             fprintf (stderr, "%s: %d: unterminated line: %s\n",
                      progname, linenr, buffer);
          }
          insertAscending( list_ptr, buffer );
    
    
          
       }
    
       for (node_ref curr = list_ptr->head; curr != NULL; curr = curr->link) 
       {
          printf ("%s\n", curr->string);
       }
    
       return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    Code:
       temp->string = input;
    is a pointer operation (as both temp->string and input are of type char *). Copying a pointer does not create a copy of the thing pointed to; it makes a new pointer that points to the same thing as the old one. Effectively what you are doing is making a list where all nodes have their string member containing the same address.

    If you want to copy a C-style string, allocate a buffer and use strcpy(). For example;
    Code:
        temp->string = malloc(strlen(input) + 1);
        if (temp->string != NULL) strcpy(temp->string, input);
    It is also probably not a good idea to have the head of a list being a local variable (to main()) and all other elements of the list() being malloc()'d. If you insert a node in front of the head, you cannot deallocate the list (as you will not know which node was not dynamically allocated).
    Last edited by grumpy; 11-07-2011 at 01:02 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You're allocating space for the node, but you're never allocating space for the string that you're trying to store in the node. All of your nodes will point their 'string' pointer at the same buffer.
    Once you've allocated space for that then you'll want to use strcpy to make a copy of the string.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    Ah thank you guys, this was what I was missing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How many pointer are pointing to this object
    By Hannibal2010 in forum C Programming
    Replies: 7
    Last Post: 10-10-2011, 09:10 PM
  2. Pointer pointing to itself
    By rrahulvverma in forum C Programming
    Replies: 20
    Last Post: 07-19-2010, 10:10 AM
  3. function pointer not pointing
    By darfader in forum C Programming
    Replies: 3
    Last Post: 09-17-2003, 03:31 AM
  4. Pointing a function pointer to a variable?
    By Aidman in forum C++ Programming
    Replies: 9
    Last Post: 07-06-2003, 11:50 PM
  5. pointer which is pointing to new~
    By black in forum C++ Programming
    Replies: 12
    Last Post: 05-10-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21