• C

void pointers

Hi,

Im having difficulties working with void pointer arguments and doubly linked lists containing data fiels of type void *. The below code works well with structures and strings, however, floats and ints escape me. the functions are to be generic, in that they are to be able to handle data objects of any type. I would be very greatful if someone could point out the error of my ways and help me solve this problem.
-------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct list DLL;
typedef struct node NODE;

struct node {
      void *data;
      NODE *prior,
              *next;
};

struct list {
      long int count;
      NODE *head,
              *tail;
};

typedef struct {
      char name[20];
      int age;
} PERSON;



void   init_list  (void *list);
void   insert     (void *list, void *inData, size_t size);
long   count      (void *list);
void  *show       (void *list);


void main(void)
{
      DLL iList, *iListP = &iList;

      int i = 34;
      char *s = "hello";
      float f = 10.2;
      PERSON kids[2] = {{ "justin", 10 },
                                          { "paul", 12}}, *kp = &kids[0];


      init_list(iListP);

      insert(iListP, &i, sizeof(int));

      printf("%d\n", (int *)show(iListP));


}


void init_list (void *list)
{
       DLL *tmp = (DLL *)list;

       tmp->head = NULL;
       tmp->tail = NULL;
       tmp->count = 0;
}



void insert (void *list, void *inData, size_t size)
{
      NODE *newn;
      DLL *tmp = (DLL *)list;

      if (!(newn=(NODE *)malloc(sizeof(NODE))))
      {
            perror("insert: malloc");
            return;
      }

      newn->data = malloc(size);
      if (!memcpy(newn->data, inData, size))
      {
            perror("insert: memcpy");
            return;
      }

      if (!tmp->head)
      {
            newn->prior = newn->next = NULL;
            (NODE *)tmp->head = (NODE *)tmp->tail = newn;
      }
      else
      {
            newn->prior = (NODE *)tmp->tail;
            newn->next = NULL;
            (NODE *)tmp->tail->next = newn;
            (NODE *)tmp->tail = (NODE *)tmp->tail->next;
      }

      (tmp->count)++;
}


long count(void *list)
{
      DLL *tmp = (DLL *)list;

      return tmp->count;
}


void *show(void *list)
{
      DLL *tmp = (DLL *)list;

      return tmp->head->data;
}

-------------------


Many thanks
Drabaecus
drabaecusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

drabaecusAuthor Commented:
Edited text of question.
0
drabaecusAuthor Commented:
Edited text of question.
0
ToronadoCommented:
Hi drabaecus,

Exactly what is going wrong here? Do you get an error, or does
printf("%d\n", (int *)show(iListP);
print something stranges?
Note : don't forget to dereference the int *. The line above will print the address of the int.
Use
printf("%d\n", *((int *)show(iListP)));

Regards,
 Toronado.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

drabaecusAuthor Commented:
Greetings Toronada,

I was getting, as you mentioned, the memory location, i tried what u sugested and it worked =)

Most pleased and greatful.

Submit and answer and ill gladly send u the points.

Again thank you :-)

Drabaecus
0
ToronadoCommented:
Hi drabaecus,

Glad I could help. I'll post this as an anser.

Regards,
 Toronado
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
drabaecusAuthor Commented:
Thanks once more
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.