关于C#:铸造空指针并使用格式说明符进行打印

Casting the void pointer and printing with format specifiers

好的,这是针对学校项目的,但是我的问题与特定实施无关,我也不寻求有关项目本身的任何帮助。我之所以只给出警告,是因为我想为自己在做的事情提供上下文,但这是一个关于在项目上下文中构造空指针的通用问题。我正在编写的此函数是"甚至对于项目而言,这只是我作为测试机制编写的东西,用以查看我生成的数据结构是否正常工作……这导致我遇到一个涉及printf(),格式说明符和转换的问题。我基本上必须实现一个链表(称为"队列",但实际上根本不是队列),并且编写了一个函数对其进行测试。

基本上有一个名为" Chunk"的结构,带有一些变量(第一个是较大数组中的索引,它是给定" Chunk"的第一个元素,而arr基本上是larger_array [c-> first],但实际上是一个像这样的void指针:void * arr,其中c是指向Chunk的指针),指示更大数组中的位置。因此,基本上,如果您有一个像a = {5,7,3,4,6,9,1,2}的数组,并且块大小为2,则您有4个块,每个块都有一个" arr" void指针变量,分别指向5、3、6、1。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 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
"
, c->arr);
    else if (elem_size == INT_SIZE) /* INT_SIZE defined above as 4 */
      printf("%d
"
, c->arr);
    else if (elem_size == CHAR_SIZE) /* CHAR_SIZE defined above as 1 */
      printf("%c
"
, c->arr);

    index++;

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

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

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

输出:

1
2
3
4
 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

这样就行了!好极了!但是,我得到这些编译器警告:

1
2
3
4
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指针"的警告,所以我做了然后:

1
 * ((type *) c->arr)

基本上取消引用我的强制转换的void指针(永远不会为null!它总是指向有效数字,至少使用我提供的输入!),然后这给我带来了分段错误!因此,我感到非常沮丧,因为我从带有大量"警告"的工作输出变为无用的分段错误。

编辑:

根据要求定义数据结构:

1
2
3
4
5
typedef struct chunk {
  void *arr;
  struct chunk *next;
  int first;
} Chunk;

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  while (index < number_of_chunks) {
    if (index == 0) {
      if ((current = malloc(sizeof(Chunk))) == NULL)
        err(EX_OSERR,"memory allocation has failed
"
);
      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
"
);
      current = current->next;
   }
  else {
    tail = current;
  }
  index += 1;
}


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

这里应该有一个&。 您想将字节的地址分配给arr而不是它的值。

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

如果您这样做,那么arr将包含一个应有的地址,这将使强制类型转换和取消引用生效。

1
2
3
4
5
6
printf("%ld
"
, *(long *) c->arr);
printf("%d
"
,  *(int  *) c->arr);
printf("%c
"
,  *(char *) c->arr);