1

好的,这是针对学校项目的,但我的问题与具体实施无关,我也没有寻求有关项目本身的任何帮助。我只是给出这个警告,因为我想为我正在做的事情提供上下文,但这是一个关于在项目上下文中构建 void 指针的一般性问题......我正在编写的这个函数不是甚至对于该项目,这只是我作为测试机制编写的东西,以查看我的数据结构生成是否正常......它导致我遇到涉及 printf()、格式说明符和强制转换的问题......我基本上必须实现一个链表(称为“队列”,但实际上根本不是队列),并且我编写了一个函数来测试它。

基本上有一个名为“Chunk”的结构,其中包含一些变量(first是较大数组中的索引,它是给定“Chunk”的第一个元素,而arr基本上是 large_array[c->first],但它实际上是void pointer as such: void *arr where c is a pointer to a Chunk) 指示更大数组中的位置。所以基本上如果你有一个像 a = {5,7,3,4,6,9,1,2} 这样的数组,并且你的块大小为 2,那么你有 4 个块,每个块都有一个“arr” void 指针变量,分别指向 5、3、6、1。

无论如何,我写了一个“print_queue”函数(实际上是一个块的链表),耶!它会根据需要打印所有信息,这是我将分享的核心部分:

 while (index < num_chunks) {
    first = c->first;
    printf("Chunk %d: first is %d and a[%d] is ", index, first, first);
    if (elem_size == LONG_SIZE) /* LONG_SIZE defined above as 8 */
      printf("%ld\n", c->arr);
    else if (elem_size == INT_SIZE) /* INT_SIZE defined above as 4 */
      printf("%d\n", c->arr);
    else if (elem_size == CHAR_SIZE) /* CHAR_SIZE defined above as 1 */
      printf("%c\n", c->arr);

    index++;

    if (c->next != NULL)
      c = c->next;
 }

我基本上想编写一个函数,该函数能够在实现项目的实际功能(多线程合并排序)时为测试目的打印三种类型(long、int 和 chars)中的任何一种的链表。所以上面的代码确实有效!这是此数组输入的输出:

 char original[] = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's'};

输出:

 Chunk 0: first is 0 and a[0] is z
 Chunk 1: first is 2 and a[2] is x
 Chunk 2: first is 4 and a[4] is v 
 Chunk 3: first is 6 and a[6] is t

所以它有效!耶!但是,我收到以下编译器警告:

mergesort.c: In function 'print_chunk_queue':
mergesort.c:85:7: warning: format '%ld' expects argument of type 'long int', but     argument 2 has type 'void *' [-Wformat]
mergesort.c:87:7: warning: format '%d' expects argument of type 'int', but argument 2 has type 'void *' [-Wformat]
mergesort.c:89:7: warning: format '%c' expects argument of type 'int', but argument 2 has type 'void *' [-Wformat]

所以我所做的是将所有 c->arr 转换为 (type *) c->arr,但这给了我更多关于“哦,我们想要一个 int 但你有一个 int 指针”的警告,所以我所做的是然后:

 * ((type *) c->arr)

基本上取消引用我的强制转换的 void 指针(它永远不会为空!它总是指向有效数字,至少在我提供的输入的情况下!),然后这给了我一个分段错误!。所以我很沮丧,因为我从带有大量“警告”的工作输出变成了无用的分段错误。

编辑:

根据要求定义数据结构:

typedef struct chunk {
  void *arr;
  struct chunk *next;
  int first;
} Chunk;

这就是我设置单个块的状态并创建块的链接列表的方式:

  while (index < number_of_chunks) {
    if (index == 0) {
      if ((current = malloc(sizeof(Chunk))) == NULL)
        err(EX_OSERR, "memory allocation has failed\n");
      head = current;
    }
    current->first = (chunk_size * index);
    current->arr = ((char *) array)[current->first];
    current->size = chunk_size;
    if (index != (number_of_chunks - 1)) {
      if ((current->next = malloc(sizeof(Chunk))) == NULL)
        err(EX_OSERR, "memory allocation has failed\n");
      current = current->next;
   }
  else {
    tail = current;
  }
  index += 1;
}
4

1 回答 1

1
current->arr = ((char *) array)[current->first];

这里应该有&。您希望将字节的地址arr分配给而不是其

current->arr = &((char *) array)[current->first];

如果您这样做,那么arr将包含一个应有的地址,这将允许强制转换和取消引用工作。

printf("%ld\n", *(long *) c->arr);
printf("%d\n",  *(int  *) c->arr);
printf("%c\n",  *(char *) c->arr);
于 2013-08-12T22:50:24.127 回答