i need to create a data type (struct in this case) with an array as a property. I have an initialiser function that initialises this data structure and gives the array a specified size. The problem now is declaring the array in the struct. for example "int values[]" will require that I enter a number in the brackets eg values[256]. Th3 256 should be specified wen the structure is initialised. Is there a way I get around this?
我需要创建一个数据类型(本例中为struct),其中数组作为属性。我有一个initialiser函数,它初始化这个数据结构并给数组一个指定的大小。现在的问题是在结构体中声明数组。例如,“int values[]”要求我在括号中输入数字eg values[256]。当结构被初始化时,应该指定th3256。我有办法解决这个问题吗?
typedef struct
{
int values[]; //error here
int numOfValues;
} Queue;
3 个解决方案
#1
16
A struct must have a fixed size known at compile time. If you want an array with a variable length, you have to dynamically allocate memory.
结构体必须在编译时具有已知的固定大小。如果想要一个可变长度的数组,就必须动态地分配内存。
typedef struct {
int *values;
int numOfValues;
} Queue;
This way you only have the pointer stored in your struct. In the initialization of the struct you assign the pointer to a memory region allocated with malloc:
这样,您就只能将指针存储在结构体中。在struct的初始化过程中,将指针分配给分配给malloc的内存区域:
Queue q;
q.numOfValues = 256;
q.values = malloc(256 * sizeof(int));
Remember to check the return value for a NULL
pointer and free()
any dynamically allocated memory as soon as it isn't used anymore.
记住,要检查空指针的返回值,并在不再使用任何动态分配内存时检查free()。
#2
8
#include<stdio.h>
#include<stdlib.h>
typedef struct Queue {
int numOfValues;
int values[0];
} Queue_t;
int main(int argc, char **argv) {
Queue_t *queue = malloc(sizeof(Queue_t) + 256*sizeof(int));
return (1);
}
This way you can declare 'variable length arrays'. And you can access your array 'values' with queue->values[index]
这样就可以声明“可变长度数组”。您可以使用队列->值来访问数组的值[索引]
EDIT: Off course you need to make sure that once you free you take into account the 'n*sizeof(int)' you have allocated along with sizeof(Queue_t) where n=256 in the above example HTH
编辑:Off course,你需要确保一旦你*了,你就会考虑到“n*sizeof(int)”,你已经和sizeof(Queue_t)一起分配了,在上面的例子中,n=256。
#3
3
You can use C99 features like VLA, e.g.
你可以使用C99特性,比如VLA。
int main()
{
int len=1234;
struct MyStruct {int i,array[len];} var;
var.array[0]=-1;
var.array[len-1]=1;
printf( "%i %i %lu", var.array[0],var.array[len-1],(unsigned long)sizeof(var) );
return 0;
}
#1
16
A struct must have a fixed size known at compile time. If you want an array with a variable length, you have to dynamically allocate memory.
结构体必须在编译时具有已知的固定大小。如果想要一个可变长度的数组,就必须动态地分配内存。
typedef struct {
int *values;
int numOfValues;
} Queue;
This way you only have the pointer stored in your struct. In the initialization of the struct you assign the pointer to a memory region allocated with malloc:
这样,您就只能将指针存储在结构体中。在struct的初始化过程中,将指针分配给分配给malloc的内存区域:
Queue q;
q.numOfValues = 256;
q.values = malloc(256 * sizeof(int));
Remember to check the return value for a NULL
pointer and free()
any dynamically allocated memory as soon as it isn't used anymore.
记住,要检查空指针的返回值,并在不再使用任何动态分配内存时检查free()。
#2
8
#include<stdio.h>
#include<stdlib.h>
typedef struct Queue {
int numOfValues;
int values[0];
} Queue_t;
int main(int argc, char **argv) {
Queue_t *queue = malloc(sizeof(Queue_t) + 256*sizeof(int));
return (1);
}
This way you can declare 'variable length arrays'. And you can access your array 'values' with queue->values[index]
这样就可以声明“可变长度数组”。您可以使用队列->值来访问数组的值[索引]
EDIT: Off course you need to make sure that once you free you take into account the 'n*sizeof(int)' you have allocated along with sizeof(Queue_t) where n=256 in the above example HTH
编辑:Off course,你需要确保一旦你*了,你就会考虑到“n*sizeof(int)”,你已经和sizeof(Queue_t)一起分配了,在上面的例子中,n=256。
#3
3
You can use C99 features like VLA, e.g.
你可以使用C99特性,比如VLA。
int main()
{
int len=1234;
struct MyStruct {int i,array[len];} var;
var.array[0]=-1;
var.array[len-1]=1;
printf( "%i %i %lu", var.array[0],var.array[len-1],(unsigned long)sizeof(var) );
return 0;
}