简体   繁体   English

如何在C中打印一个双指针值

[英]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". 我们正在为C中的通用数据类型实现优先级队列。我们认为指针等之间的分配是正确的,但是我们不知道如何在末尾打印“ element”的int值。 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. 好吧,由于从未定义过pqElement类型,而仅定义了_pqElement结构,因此即使是现在的代码也无法编译。

Also you are using %d in the printf , but the parameter you are passing is void** , so you need to cast the value. 另外,您在printf中使用%d ,但是传递的参数为void** ,因此需要强制转换值。

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! 注意:正如我在代码中的注释中所报告的那样,您需要释放使用malloc分配的内存,否则可能会发生内存泄漏!

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. 最后,如亚历山大·潘(Alexander Pane)所述,打印值需要以正确的方式转换指针。

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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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