void types

This is a discussion on void types within the C Programming forums, part of the General Programming Boards category; Ok, so I made a linked list in C, and I'm trying to make it store datatypes that the List ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    6

    void types

    Ok, so I made a linked list in C, and I'm trying to make it store datatypes that the List doesn't necessarily know about (I guess generic programming?). So far I have tried using the datatype "void" to accomplish this and it "kind of" works. I'll explain it down below. I'm not sure what the problem is, or if my method of doing this is even possible.

    Below is some of the code which is outside of the Linked List

    Note: I do eventually want to store more than a single integer in Node, but for simplicity/testing this is what I have:
    Code:
    typedef struct Node {
        int value;
    } Node;
    typedef Node* NodeRef;
    ...blah blah ...
    Code:
        ListRef list = newList();
        NodeRef node;
        int i = 0;
        for (i = 0; i < n; i += 1) {
            node = malloc(sizeof (Node));
            node->value = i + 1;
            insertAfterLast(list, &node);
        }
    and here is the code that is a part of my linked list:
    Code:
    typedef struct Node {
        void *data;
        struct Node* next;
        struct Node* prev;
    } Node;
    
    typedef Node* NodeRef;
    
    typedef struct List {
        NodeRef front;
        NodeRef current;
        NodeRef back;
        int length;
    } List;
    
    void insertAfterLast(ListRef L, void *data) {
        if (L == NULL) {
            printf("List Error: calling insertAfterLast on Null ListRef");
            exit(1);
        }
        NodeRef N;
        N = malloc(sizeof (Node));
        N->data = data;
        N->next = NULL;
        N->prev = L->back;
        if (isEmpty(L)) {
            L->front = N;
        } else {
            L->back->next = N;
        }
        L->back = N;
        L->length += 1;
    };
    Both the linked list code, and the first part of my code are in their own files and have their own header files etc etc..

    What is weird is that this works except that all the entries in my list have a value of n+1. (For instance, if n = 7 then every element in my linked list has a value of 7). It seems like every element of my list is referencing the same address. I'm wondering if anyone can explain why this is happening.

    Please let me know if you need to see more code.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    I would like to point out that it is extreme poor practice to typedef a pointer. As for:
    What is weird is that this works except that all the entries in my list have a value of n+1.
    I would point you to your own code:
    Code:
     for (i = 0; i < n; i += 1) {
            node = malloc(sizeof (Node));
            node->value = i + 1;
            insertAfterLast(list, &node);
    }
    Last edited by AndrewHunter; 07-25-2011 at 05:34 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How are you getting the values back out? I don't see any obvious errors here, although the two different struct Node types running around are confusing.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Quote Originally Posted by tabstop View Post
    How are you getting the values back out? I don't see any obvious errors here, although the two different struct Node types running around are confusing.
    Sorry, being a little lazy I guess, but they are in separate files. Here is the code I'm using to retrieve them:

    Code:
    NodeRef test = *(NodeRef *) getFirst(list);
    and from list:

    Code:
    void* getFirst(ListRef L) {
        if (L == NULL) {
            printf("List Error: calling getFirst() on Null ListRef");
            exit(1);
        } else if (isEmpty(L)) {
            printf("List Error: calling getFirst() on Empty List");
            exit(1);
        }
        return L->front->data;
    };
    I should point out that I tested the List separately earlier and it was working fine when storing just integers.

    Also AndrewHunter,

    How do you propose i have n different pointers of increasing order if I can't use something like i + 1?

    would something like this work, or am I missing the point?:
    Code:
    int temp = i + 1;
    node->value = temp;

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Solved:

    I changed Node to NodeData to help distinguish the two.

    Was:
    Code:
    insertAfterLast(list, &node);
    Is now:
    Code:
    insertAfterLast(list, node);
    Was:
    Code:
    NodeRef test = *(NodeRef *) getFirst(list);
    Is now:
    Code:
    NodeDataRef temp = (NodeDataRef) getCurrent(list);
    Thanks for all your help everyone =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  2. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  3. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  4. Types, Integral Types, Bytes?!?!?!
    By Kaidao in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2006, 07:15 AM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM

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