Thread: A problem with pointers

  1. #1
    Registered User
    Join Date
    Oct 2007

    A problem with pointers

    Hi. I'm a beginner in C and I'm having a problem in a function. It's maybe an obvious thing but I can't see what it is. The program compiles fine, the problem is in the expected result.

    I parse a file with 2 columns of words (key and value). For each line, I store the key in a hash trie, and the associated value is stored in the last node of the key in the trie.

    The function 'followlinks()' receives a word, creates the nodes/edges in trie, and returns the last node in the path. The node has a 'data' field to store a value. The problem is that it looks that the value stored in the nodes gets overriden with the value in each subsequent line processed. I suspect it's I'm messing things up with pointers.

    I'd massively appreciate any hints. Here is some of the code. If you need it i can attach the 2 files.

    int main(int argc, char **argv) {
        char s[LINESZ];
        hashtrie *ht = new_hashtrie();
        FILE *in = fopen(WORDFILE, "r");  /* e.g. /usr/share/dict/words */
        char *m; 
        if (!in) {
          return 1;
    while (fgets(s, LINESZ, in)) {
        int len=strlen(s);
        trienode *node;
        if (len) { 
        char *p;
        p = strtok (s,"\t");  /* splits string using tab delimiter */
        node=followlinks(ht, 0, p, 1);  	/* add the key in colum1 to the trie */
        p = strtok (NULL, "\t");  /* gets the second part of the string i.e. the value */  
        node->data=p; /* assigns the value to the returned node */
    Here is the code that creates the links (i.e. edges) in the trie:

    trienode *followlink(hashtrie *ht, trienode *start, char c, int create) {
      trielink **a = ht->links + hash(start, c);
      while (*a) {
        if (((*a)->source == start) && ((*a)->label == c))
          return (*a)->destination;
        a = &((*a)->next);
      if (!create) return 0;
      /* If I reach this point means that 'create' value is 1 */
      *a = malloc(sizeof(trielink));
      (*a)->source = start;
      (*a)->label = c;
      (*a)->destination = malloc(sizeof(trienode));
      (*a)->destination->data = 0;
      return (*a)->destination;  
    trienode *followlinks(hashtrie *ht, trienode *start, char *s, int create) {
      while (*s) {
        start = followlink(ht, start, *s++, create);
        if (!start) break;
      return start;
    And here are the definitions of the structs that appear above:

    typedef struct trienode { void *data;} trienode;
    typedef struct trielink {
      struct trielink *next;
      trienode *source;
      char label;
      trienode *destination;
    } trielink;

  2. #2
    Registered User
    Join Date
    Oct 2001
    > node->data=p; /* assigns the value to the returned node */
    You assign the address of p to data, but what you really want is to copy the string at p. But first you need to allocate some memory for this string. So to do this:
    node->data = malloc(strlen(p)+1);
    strcpy(node->data, p);
    And don't forget to #include <string.h> at the top, if you haven't already.

  3. #3
    Registered User
    Join Date
    Oct 2007
    swoopy, many thanks for the reply. That solved the problem . I understand now the mistake I was making. Thanks.

    Last edited by vsla; 10-10-2007 at 04:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hi , i'm having a problem with pointers in C ...
    By blackslither in forum C Programming
    Replies: 2
    Last Post: 10-26-2008, 03:57 AM
  2. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  3. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM