简体   繁体   中英

How to print a double pointer value in C

we are implementing a priority queue for generic type of datas in C. We think the assignments between pointers etc. are made right, but we don't get how to print at the end the int value of "element". Can you help me?

#include <stdio.h>
#include <stdlib.h>

struct _pqElement{
  void** data;
  void** priority;
};

pqElement* pqElement_new(void* data, void* priority){
  pqElement* result = (pqElement*) malloc (sizeof(pqElement));
  result->data=&data;
  result->priority=&priority;
  return result;
}

static int* new_int(int value){
 int *elem=(int*)malloc(sizeof(int));
 *elem=value;
 return elem;
}

int main(int argc, char const *argv[]){
  pqElement * element = pqElement_new(new_int(1), new_int(85));
  printf("%d-%d", element->data, element->priority);
}

Well the code as it is does not even compile, since the pqElement type has never been define, but only the _pqElement struct has been defined.

Also you are using %d in the printf , but the parameter you are passing is void** , so you need to cast the value.

These changes should make the trick:

#include <stdio.h>
#include <stdlib.h>

typedef struct _pqElement{
    void** data;
    void** priority;
} pqElement;

pqElement* pqElement_new(void* data, void* priority){
    static pqElement* result;
    result = (pqElement*) malloc (sizeof(pqElement));
    result->data=&data;
    result->priority=&priority;
    return result;
}

int* new_int(int value){
    static int *elem;
    elem = (int*)malloc(sizeof(int));
    *elem=value;
    return elem;
}

int main(int argc, char const *argv[]){
    pqElement *element = pqElement_new(new_int(1), new_int(85));
    printf("%d-%d\n", **((int**)(element->data)), **((int**)(element->priority)));
    //need to free the memory allocated with the malloc, otherwise there is a possibility of memory leakage!
}

This will only print the first element, but you can point to the following elements by using an offset.

NOTE: As I reported as a comment in the code, you need to free the memory allocated using the malloc, otherwise you have potential memory leakage!

You don't need two levels of pointers.

#include <stdio.h>
#include <stdlib.h>

struct pqElement{
  void *data;
  void *priority;
};

struct pqElement* pqElement_new(void* data, void *priority)
{
  struct pqElement* result = malloc(sizeof(struct pqElement));
  result->data = data;
  result->priority = priority;
  return result;
}

static int* new_int(int value)
{
  int *elem = malloc(sizeof(int));
  *elem=value;
  return elem;
}

int main(int argc, char const *argv[])
{
  struct pqElement *element = pqElement_new(new_int(1), new_int(85));
  printf("%d-%d", *(int*)element->data, *(int*)element->priority);
}

Finally printing the value requires to cast the pointer in a proper way, as Alexander Pane already mentioned.

Using different type for priority as well will make the queue a bit less generic. You need to provide different functions for sorting, printing, etc.

Thank you, guys. I need the code works also with strings so I did like this:

static char* new_string(char* value){
  char* elem= malloc (sizeof(char));
  strcpy(elem, value);
  return elem
}

Printing out like this:

 int main(int argc, char const *argv[]){
   struct pqElement *element = pqElement_new(new_string("Hello"), 85);
   printf("%s-%d", *(char*)element->data, element->priority);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM