Thread: printing from void pointer

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    157

    printing from void pointer

    Code:
    int main()
    {
    	List* newList= lst_new();
    	names* nama;
    	char* data;
    	int x=1;
    	
    	while(x == 1)
    	{
    	nama = names_new();
    	lst_add(newList,nama);
    	
    	for( data = lst_first(newList);
    					data != NULL;
    						data = lst_next(newList))	 
    		printf("-- %s --\n",(char*)data);  /* prints (giberish)*/
    		printf("\n");
    	
    	
    	names_delete(nama);
    	
    	printf("x = ?? :");
    	scanf("%d",&x);
    	
    	while(getchar() != '\n');
    	}
    	return 0;
    }
    i cant seem to be able to print a string, can any one tell me where im going wrong.. the functions lst_next() lst_first() return void*.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Quote Originally Posted by africanwizz
    the functions lst_next() lst_first() return void*.
    Why do they return void*? I would expect them to return say, a pointer to a node in the linked list (if I guessed correctly that you are trying to implement a linked list). If so, then you should dereference that pointer to get the data pointer, and cast that data pointer to char*. Furthermore, the pointer named data in main should probably be renamed to node or current or temp.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    yea, its a linked list attempt
    Quote Originally Posted by laserlight View Post
    Why do they return void*?
    "If so, then you should dereference that pointer to get the data pointer, and cast that data pointer to char*." , thats actually what the functions do. the data pointer is a void pointer hence the return type

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Quote Originally Posted by africanwizz
    "If so, then you should dereference that pointer to get the data pointer, and cast that data pointer to char*." , thats actually what the functions do. the data pointer is a void pointer hence the return type
    Yes, but I am saying that the functions should not return void*.

    Put it another way: show the implementation of lst_next(). Show also the definition of List.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Code:
    /*
     * Position iterator to head of list and return its data.
     */
    void *lst_first(List *lst)
    {
        lst->iterator = lst->head;
        if(lst->iterator != NULL)
            return nd_getData(lst->iterator);
        else
            return NULL;
    }
    
    
    /*
     * Position iterator to next node in list and return its data.
     */
    void *lst_next(List *lst)
    {
        if(lst->iterator != NULL)
        {
            lst->iterator = nd_getNext(lst->iterator);
            if(lst->iterator != NULL)
                return nd_getData(lst->iterator);
        }
        return NULL;
    }
    and the list
    Code:
    /* Linked list data */
    typedef struct
    {
        Node *head; /* First element in list */
        Node *tail; /* last element in list */
        Node *iterator; /* Used to iterate through list */
        int size;   /* number of elements currently in list */
    } List;

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    What you have shown looks like it could be correct. The problem could lie in lst_add. I don't know, because there is so much necessary code that you did not show.

    I suggest that you post the smallest and simplest compilable program that demonstrates the problem.

    EDIT:
    Another thing to look at is your use of the names type. You wrote:
    Code:
    lst_add(newList,nama);
    where nama is a pointer to an object of type names. What is this names type?
    Last edited by laserlight; 05-26-2014 at 11:40 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    alright thanks,will look into it. for now sleep whispers sweet sounds in my ear

  8. #8
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Code:
    return nd_getData(lst->iterator);
    What is getdata returning?

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Thanks for the help. problem was with some of my system settings

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You are returning a `void *' from an interface called `lst_next' which is part of a component providing a "linked list".

    I don't know what your problem was, but I know that you still have a problem if you haven't made the changes implied by laserlight.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2014, 05:46 PM
  2. Void function not printing to screen. Plz Help!!!
    By c++help in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2011, 01:07 AM
  3. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  4. fprintf in void function Not Printing
    By thetinman in forum C Programming
    Replies: 5
    Last Post: 10-17-2006, 05:13 PM
  5. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM