Passing an array to a linked list

This is a discussion on Passing an array to a linked list within the C Programming forums, part of the General Programming Boards category; Hi, i am trying to use a linked list in my code but i am having some difficulties. The link ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Passing an array to a linked list

    Hi, i am trying to use a linked list in my code but i am having some difficulties. The link is defined like that:

    Code:
    struct kword {
    	unsigned char *word;
    	struct kword *next;
    };
    I am trying to pass the data of a two dimensional array row by row onto this unsigned char *word, but i can't find a way to do it.

    I copy the two dimensional array on to a one dimensional array:

    Code:
    char pattern_small[m];
        
    for( i = 0; i < 5; i++)
        pattern_small[i] = pattern[0][i];
    i create a pointer to this 1d array:

    Code:
    char *test = pattern_small;
    and i assign this pointer to the list:

    Code:
    struct kword *khead, *ktemp;
    
    ktemp = ( struct kword * ) malloc ( sizeof ( struct kword ));
    ktemp->word = test;
    ktemp->next = khead;
    khead = ktemp;

    But this doesn't work.

    pattern_small[0]-[5] holds the string "abcde". If i pass the string directly (ktemp->word = "abcde"; i get correct results instead of using the pointer.

    I would appreciate for any ideas on this!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    We probably need to see a bit more of your code to say exactly what is wrong. I suspect, however, that you are storing the address of pattern_small, and then overwriting pattern_small with something else. Since all the "word" pointers are pointing to the same thing, you only get one lot of data in your list [and if it's a local variable, once that is out of scope it may contain any random rubbish] Is this what you are seeing?

    To solve that particular problem, you would need to allocate memory for the string and copy the string into your new structure. Or if your strings are of reasonable size, make a fixed size array of (say) 25 chars as your "word", and copy into that [note that malloc will often use large areas per allocation, so allocating 6-10 bytes or allocating 25 bytes will be the same amount - in visual studio .Net, malloc's smallest allocation appears to be 64 bytes].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Thanks for the very fast answer!

    Essentially the problem is how to copy the string stored in the array small_pattern[] into the unsigned char *word. A *word pointer is created for each string i pass, since this is a linked list.

    I can paste larger parts of the code if needed.

    Thanks!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but what does the word pointer actually point to? Is that THE same place in memory or different places in memory?

    You could easily check that with:
    Code:
      ... 
    ktemp = ( struct kword * ) malloc ( sizeof ( struct kword ));
    ktemp->word = test;
    ktemp->next = khead;
    if (khead != 0 && khead != ktemp) {
       if (khead->word == ktemp->word) {
          printf("Words point to the same thing - this is not valid\n");
       }
    }
    khead = ktemp;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    As i wrote in the beginning, this is a linked list:

    Code:
    ktemp->word = test; <- assign the string to the unsigned char *
    ktemp->next = khead;khead = ktemp; <- point ktemp to the next element in the list

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and your point is?

    Whether it's a linked list, and array, a hash-table or some other construction, if you are storing a string as a char *, each value of char * needs to point to a specific place which contains the string you refer to.

    To make a simple example (try this on your system)
    Code:
    char *arr[3];
    
    int main()
    {
        char str[10] = "Hello";
        for(i = 0; i < 3; i++)
           arr[i] = str;
        strcpy(arr[2], "World");
        for(i = 0; i < 3; i++)
           printf("arr[%d]=%s\n", i, arr[i]);
        return 0;
    }
    Now do this (changes in red):
    Code:
    char *arr[3];
    
    char *copystr(char *s)
    {
        char *p = malloc(strlen(s)+1);
        strcpy(p, s);
        return p;
    }
    
    int main()
    {
        char str[10] = "Hello";
        for(i = 0; i < 3; i++)
           arr[i] = copystr(str);
        strcpy(arr[2], "World");
        for(i = 0; i < 3; i++)
           printf("arr[%d]=%s\n", i, arr[i]);
        return 0;
    }
    The same would happen if "str" was inserted into a linked list. I choose an array to make it simple to understand.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    I think your answer solved the problem for me:

    When i was doing this:

    Code:
            char pattern_small[m];
    	
    	for( i = 0; i < m; i++)
    		pattern_small[i] = pattern[0][i];
    	
    	ktemp = ( struct kword * ) malloc ( sizeof ( struct kword ));
    	ktemp->word = pattern_small;
    	ktemp->next = khead;
            khead = ktemp;
    	
    	printf("ktemp->word = %s\n",ktemp->word);
    i was getting this result: ktemp->word = abcde��

    So i changed the allocation of pattern_small a bit:

    Code:
            char *pattern_small;
    	
    	pattern_small = malloc (m + 1);
    	
    	for( i = 0; i < m; i++)
    		pattern_small[i] = pattern[0][i];
    	
    	ktemp = ( struct kword * ) malloc ( sizeof ( struct kword ));
    	ktemp->word = pattern_small;
    	ktemp->next = khead;
    	khead = ktemp;
    	
    	printf("ktemp->word = %s\n",ktemp->word);
    so now ktemp->word = abcde

    I am not sure i fully comprehend pointers, i need a lot more studying, but at least i am starting to understand how they work.

    Thanks a lot for your help!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so a pointer is just a memory address stored in a variable. One piece of memory can only store ONE value. Since your pattern_small variable is still the same place in memory each time you insert it into the linked list, although it contains different things each time you insert it, you actually overwrite the previous value each time, and this means that your linked list always contains one thing all throughout the list (many pointers, but all to the same place).

    Think of a pointer as the address of a house, and the memory content is who lives in the rooms. If you have an address of 10 Kensington Avenue, London, then some people live there [and in this computer version of houses, you can't have more than one person living in each room]. You can change who lives there, but ultimately, only a certain set of people live there. So your pointer contains the address of the house. The string is the people living in the house. Does this make sense?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array to linked list
    By bar338 in forum C Programming
    Replies: 7
    Last Post: 04-08-2009, 10:15 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 08:11 AM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM

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