working with void stars???

This is a discussion on working with void stars??? within the C Programming forums, part of the General Programming Boards category; I really dont understand the typecasting part of this... Im getting alot of errors.. Can anyone tell me why and ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    working with void stars???

    I really dont understand the typecasting part of this... Im getting alot of errors..
    Can anyone tell me why and how I can fix it?

    ** I have to use the void stars..

    Code:
    typedef struct _stacknode{
        void *value;
        struct _stacknode *next;
        struct _stacknode *prev;
    }STACKNODE;
    
    typedef struct _stack{
        struct _stacknode *sent;
    }STACK;
    
    void push(void *value, void *stack)
    {
        STACK *new_node;
        new_node = (STACK *)malloc(sizeof(STACK));
        stack->prev->next = new_node;
        new_node->prev = stack->prev;
        stack->prev = new_node;
        new_node->next = stack;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The only typecasting you're doing is in the new_node = (STACK *) malloc etc. line, and coincidentally that typecast is incorrect.

    Edit: Perhaps not coincidentally, new_node needs to be of type STACKNODE *.
    Last edited by tabstop; 10-24-2008 at 11:20 AM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by tabstop View Post
    The only typecasting you're doing is in the new_node = (STACK *) malloc etc. line, and coincidentally that typecast is incorrect.

    Edit: Perhaps not coincidentally, new_node needs to be of type STACKNODE *.
    Well what about stack->prev->next... it says that I cant do that.. im thinking bc its a void * and it doesnt really have a prev pointer. how do I type cast or make the void *stack into a STACKNODE ??

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You do not need the typecast. The typecast actually negates the entire purpose of void pointers. They are a wild card type of data. They are basically pointers to anything. And I would imagine tabstop's point is correct too. Especially when considering that a STACK does not have any members named prev or next.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by scarlet00014 View Post
    Well what about stack->prev->next... it says that I cant do that.. im thinking bc its a void * and it doesnt really have a prev pointer. how do I type cast or make the void *stack into a STACKNODE ??
    Example:
    Code:
    void push(void *value, void *stack)
    {
        STACKNODE *new_node = malloc(sizeof(STACKNODE));;
        STACK *sstack = stack;
    
        sstack->prev->next = new_node;
        new_node->prev = stack->prev;
        sstack->prev = new_node;
        new_node->next = sstack;
    
    }
    Last edited by master5001; 10-24-2008 at 03:31 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by scarlet00014 View Post
    Well what about stack->prev->next... it says that I cant do that.. im thinking bc its a void * and it doesnt really have a prev pointer. how do I type cast or make the void *stack into a STACKNODE ??
    STACKNODE *real_stack = stack;

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    In case you are wondering why, scarlet00014 (do you really need three zeroes in your name?) the reason is simply because its cleaner to make another variable than it is to keep casting stuff. Your optimizer will work its magic in either case.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by master5001 View Post
    Example:
    Code:
    void push(void *value, void *stack)
    {
        STACKNODE *new_node = malloc(sizeof(STACKNODE));;
        STACK sstack = stack;
    
        sstack->prev->next = new_node;
        new_node->prev = stack->prev;
        sstack->prev = new_node;
        new_node->next = sstack;
    
    }
    Cool thankyou With this, I was able to correct all my other functions too.. except this one .. if I change bool to int it works.. but that shouldnt matter becuase im just returning 1 or 0 right? whats wrong with this code?
    Code:
    bool is_empty(void *stack){
        STACKNODE *current = stack;
        if(current->value == NULL){
            return 1;
        }else{
            return 0;
        }
    }

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    #include <stdbool> at the very very very top of your program. Or (and my prefered method) #define bool int

    Either/or. The stdbool thing is slightly more correct since it accomodates C++ compilers better.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by master5001 View Post
    Example:
    Code:
    void push(void *value, void *stack)
    {
        STACKNODE *new_node = malloc(sizeof(STACKNODE));;
        STACK sstack = stack;
    
        sstack->prev->next = new_node;
        new_node->prev = stack->prev;
        sstack->prev = new_node;
        new_node->next = sstack;
    
    }
    Huh?
    STACK (type) is "struct _stack" and stack (variable) is void*.
    Last edited by Elysia; 10-25-2008 at 02:26 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well aren't you a sweetheart for pointing it out in such a pleasant way. I am soooooooooo jumping all over your next typo.

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    *fight fight*

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by master5001 View Post
    Well aren't you a sweetheart for pointing it out in such a pleasant way. I am soooooooooo jumping all over your next typo.
    I'll be waiting

    Oh, and regardless of what anyone thinks, I just think it's better to do it this way:
    Code:
        STACKNODE* new_node = (STACKNODE*)malloc(sizeof(STACKNODE));
        STACK* sstack = (STACK*)stack;
    Evil void* pointers
    Last edited by Elysia; 10-25-2008 at 02:28 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm kind of wondering what the purpose of making the second parameter for push() a void * - I can understand that value is a void pointer because you want to make the functions generic. But you could easily declare a stack type, that is opaque (that is, you do not need to know what the content is outside of the stack management code itself), and then define the stack type inside the stack management code itself.

    Something like this:
    Code:
    // stack.h
    typedef struct _stack STACKNODE;
    
    void push(void *value, STACKNODE *stack);
    Code:
    // stack.c:
    struct _stacknode{
        void *value;
        struct _stacknode *next;
        struct _stacknode *prev;
    }STACKNODE;
    
    void push(void *value, STACKNODE *stack)
    {
    ...
    }
    That way, there's no need for casting of the STACK parameter.

    --
    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. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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