gpt4 book ai didi

c - 在 C 中使用可变长度指针数组时出现 gcc 错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:22:52 25 4
gpt4 key购买 nike

我收到这个错误:

list.c list.h types.h
list.c: In function 'List_push':
list.c:11:23: error: invalid initializer
--- void *values[len] = ls->values);

编辑:

现在,使用我当前的代码(我已经完成了删除某些内容的撤消/重做),我得到了这个错误:

gcc

为什么?

基本上我有一个 List 结构,它声明了一个可变长度数组,其中包含 void 指针(我想要的是指向任何数据类型的指针)。您可以在下面的 list.h 文件中看到它。

我在 list.c 中尝试了多种更改(即 *values[pos++] = ... 等...),但进行这些更改只会导致更糟的结果gcc 错误。

wscom.c

#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "types.h"

int main() {
List ls;
// TEST: Put a value pointer at index 0
uint8 value = 0x41;
List_push(&ls, 1, &value);
printf("%c",
*(char*) List_getindex(&ls, 0)
);
return 0;
}

类型.h

#ifndef hydroTrackerTypesH
#define hydroTrackerTypesH

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned long long uint32;

#endif

list.h(声明)

#ifndef hydroTrackerListH
#define hydroTrackerListH

#include "types.h"

typedef struct {
uint32 length;
void *values[];
} List;

void List_push(List *ls, uint8 count, ...);
void *List_getindex(List *ls, uint32 i);
void List_setindex(List *ls, uint32 i, void *v);

#endif

list.c(默认)

#include "list.h"
#include "types.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>

static size_t PointerSize =
sizeof(void*);

void List_push(List *ls, uint8 count, ...) {
uint32 len = ls->length;
void *values[len] = ls->values;
uint32 sum = len + count;
realloc(&values, sum * PointerSize);
ls->length = sum;

va_list newVals;
va_start(newVals, count);
uint8 pos = len;

while(count--)
values[pos++] = va_arg(newVals, void*);

va_end(newVals);
}

void *List_getindex(List *ls, uint32 i) {
return (void *)(ls->values[i]);
}

//void List_setindex(List *ls, uint32 i, void *v);

最佳答案

评论有点长。因此,我将其作为答案。

我试图向您展示指针和数组是如何相互关联的:

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

int main()
{
/* The compiler allocates space for "Hello" and '\0' (5 + 1 chars)
* and stores the address in aString1.
*/
const char *aString1 = "Hello";
/* The compiler allocates 10 chars and initializes
* it with "World" (and the '\0' for terminator).
*/
const char aString2[10] = "World";
/* The compiler determines length of initializer "I'm here."
* (9 + 1) and allocates the array of appropriate size.
*/
const char aString3[] = "I'm here.";
/* allocate storage for array (3 const char*) */
#if 0 /* the usual way */
const char **array = malloc(3 * sizeof (const char*));
#else /* how Matheus wants to do it */
const char **array = NULL;
array = realloc(array, 3 * sizeof (const char*));
#endif /* 0 */
/* assign contents (using it like an array) */
array[0] = aString1;
array[1] = aString2;
array[2] = aString3;
/* apply array to another variable array2 */
const char **array2 = array; /* assigns the address only */
/* use it: */
printf("array2[0]: '%s', array2[1]: '%s', array2[2]: '%s'\n",
array2[0], array2[1], array2[2]);
/* throw away storage of array (and array2) */
free(array);
/* Attention! array, array2 become wild pointers at this point
* and may not be accessed (except new, valid addresses are assigned).
* However, aString1, aString2, aString3 are still intact.
*/
printf("aString1: '%s', aString2: '%s', aString3: '%s'\n",
aString1, aString2, aString3);
/* done */
return 0;
}

样本可在ideone.com上进行测试.

样本输出是:

array2[0]: 'Hello', array2[1]: 'World', array2[2]: 'I'm here.'
aString1: 'Hello', aString2: 'World', aString3: 'I'm here.'

更新:

于是,我终于又看了一遍Matheus的问答。并尝试根据他的意图(或我的理解)修复它。我基于 Matheus ' 通过注释实现和注释修改代码:

list.h:

#ifndef LIST_H
#define LIST_H

#if 0 /* not necessary to define these types */
#include "types.h"
#else /* they are already available in a (better) portable manner: */
#include <stdint.h>
/* Btw. I had to change:
* uint8 -> uint8_t
* uint32 -> uint32_t
*/
#endif /* 0 */

typedef struct {
uint32_t length;
#if 0 /* gcc ERROR: */
/* list.c:17:3: error: invalid use of flexible array member
* ls->values = NULL;
*/
void *values[];
#else /* (not) 0 */
void **values;
#endif /* 0 */
} List;

void List_init(List *ls);
void List_push(List *ls, uint8_t count, ...);
void* List_getindex(List *ls, uint32_t i);
void List_setindex(List *ls, uint32_t i, void *v);

#endif /* LIST_H */

list.c:

#include "list.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

#if 0 /* no need for a variable (with storage */
static size_t PointerSize = sizeof(void*);
#else /* use enum instead (constant) */
enum { PointerSize = sizeof(void*) };
#endif /* 0 */

void List_init(List *ls)
{
ls->length = 0;
/* This is important: */
ls->values = NULL;
/* or 1st realloc() in List_push() may have Undefined Behavior.) */
}

void List_push(List *ls, uint8_t count, ...)
{
uint32_t len = ls->length;
uint32_t sum = len + count;
void **values = realloc(ls->values, sum * PointerSize);
if (!values) {
/* realloc() failed! Bail out before destroying the existing data. */
return;
}
ls->length = sum;
ls->values = values;
/* assign new contents */
va_list newVals;
va_start(newVals, count);
#if 1 /* the readable way: */
int pos = len;
while (count--) values[pos++] = va_arg(newVals, void*);
#else /* the hackish C style way: */
values += len;
while (count--) *values++ = va_arg(newVals, void*);
#endif /* 1 */
va_end(newVals);
}

void* List_getindex(List *ls, uint32_t i)
{
return ls->values[i];
}

wscom.c:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "list.h"

int main()
{
List ls;
/* Put a value pointers at indices 0, 1, 2 */
uint8_t value1 = 0x41, value2 = 0x42;
uint8_t value3[3] = { 0x43, 0x44, 0x45 };
List_init(&ls);
List_push(&ls, 3, &value1, &value2, value3);
/* Check whether list contents can be retrieved again */
if ((*(uint8_t*)List_getindex(&ls, 0)) == 0x41) {
printf("List entry 0 is correct.\n");
}
if ((*(uint8_t*)List_getindex(&ls, 1)) == 0x42) {
printf("List entry 1 is correct.\n");
}
{ uint8_t *values = List_getindex(&ls, 2);
if (values[0] == 0x43
&& values[1] == 0x44
&& values[2] == 0x45) {
printf("List entry 2 is correct.\n");
}
}
/* Done. */
return 0;
}

在我的一条评论中,我指出 struct List 中的 void *values[]; 可能没问题。咳咳,我错了。当我试图在 list.c 中使用它时,gcc 将其标记为错误。所以,实际上,它还可以,但不是我打算使用它的目的。

最后,我的示例 session (在 Windows 10 上的 cygwin 中使用 gcc):

$ gcc -std=c11 -o wscom wscom.c list.c

$ ./wscom
List entry 0 is correct.
List entry 1 is correct.
List entry 2 is correct.

$

第 2 更新:

(我相信)我意识到了 Matheus 的缺失部分 | (考虑到他的 Javascript 背景):

C 中没有动态数组(与 Javascript 相反)。相反,有一些可变大小的数组,只能在特定情况下使用:

在 C 中:

  1. 禁止在全局变量中定义可变大小的数组。 (编译器需要知道要为存储分配多少字节。)这并不排除诸如
    之类的东西int array[] = { 1, 2, 3 };
    因为编译器根据初始化程序(在 = 的右侧)确定大小。

  2. 可以声明没有明确大小的全局数组。 (具有适当大小的定义可能/必须在其他地方完成。如果找不到适当的存储定义,链接器将失败。)

  3. 局部变量(在函数内部,存储类 auto 但不是 staticextern)可能被声明为数组大小在运行时确定(来自变量)。此功能在 C99 中引入,但(尚未)在 C++ 中引入(至少在 C++11 之前是这样)

  4. 函数参数可以声明为未知(或任何)大小的数组。 (这相当于将其声明为指针。)

我在 SO: Dynamic array allocation on stack in C 中找到了一个很好的答案(我用它来证明我上面的陈述)。

在 C 中唯一支持“动态数组”的方法是使用标准库函数 malloc()/realloc()/free( )。然而,这最好称为“动态内存”分配,因为它适用于任何 C 类型(不仅是数组)。

免责声明:

如果我写了一些关于 Javascript 的垃圾内容,我深表歉意。我是 Javascript 的新手,实践经验非常少......

关于c - 在 C 中使用可变长度指针数组时出现 gcc 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43832913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com