How can I initialize an array in C such as
如何在C中初始化数组,如
void initArr(int size)
{
...
}
The C language does not give the option to initialize an array if his size is not an constant value, and if I initialize it generally (int *arr;) so it gives me error of 'arr' is not being initialized.
如果一个数组的大小不是一个常量值,那么C语言不会给出初始化数组的选项,如果我通常初始化它(int * arr;)那么它会给我错误的'arr'没有被初始化。
Similarily, how can I do that when I have an array with dimension bigger than one (matrix, for example)?
类似地,当我有一个尺寸大于1的数组(例如矩阵)时,我怎么能这样做呢?
3 个解决方案
#1
10
The answer that works in C and C++ is dynamic memory allocation
在C和C ++中有效的答案是动态内存分配
int *arr = (int*)malloc(size*sizeof(int));
In C++ you would prefer to use new instead of malloc, but the principle is the same.
在C ++中,您更喜欢使用new而不是malloc,但原理是相同的。
int* arr = new int[size];
#2
1
The C language does not give the option to initialize an array if his size is not an constant value
如果数组的大小不是常量,则C语言不提供初始化数组的选项
In C99 you can use a variable length array and then initialize it by using a loop.
在C99中,您可以使用可变长度数组,然后使用循环对其进行初始化。
if I initialize it generally (int *arr;) so it gives me error of 'arr' is not being initialized.
如果我一般初始化它(int * arr;)所以它给我错误'arr'没有被初始化。
This is because a pointer must be initialized (points to a pointee - excluding NULL) before it is being used in program.
这是因为在程序中使用指针之前必须初始化指针(指向指针对象 - 不包括NULL)。
#3
0
In C, you can initialize objects to 0 with memset
. It's not possible to use memset
to portably initialize objects other than character arrays to a repeated non-zero value.
在C中,您可以使用memset将对象初始化为0。不可能使用memset将字符数组以外的对象可移植地初始化为重复的非零值。
In C++, the same is true, but it is restricted to so-called "POD" objects (Plain Old Data), which are basically the same objects you could have in C (no virtual functions, no private data members, etc. -- the precise definition is in the standard). It's not good C++ style, but it's possible.
在C ++中,情况也是如此,但它仅限于所谓的“POD”对象(普通旧数据),它们与C中的对象基本相同(没有虚函数,没有私有数据成员等等 - - 精确定义在标准中)。它不是很好的C ++风格,但它是可能的。
In both C and C++ you can find the total size of an array in bytes (which you need to pass to memset
) by multiplying the dimensions and the size of a single data element. For example:
在C和C ++中,您可以通过将单个数据元素的维度和大小相乘来找到数组的总大小(以字节为单位)(需要传递给memset)。例如:
void InitializeMatrix(double *m, size_t rows, size_t cols) {
memset(m, 0, rows * cols * sizeof *m);
}
In C99, you can declare a variable length array (VLA), even with multiple dimensions, and if you do so, you can use the sizeof
operator directly on the array, which can be a lot more convenient. But there are lots of restrictions on VLAs; they often don't work the way you expect them to. However, the following does work:
在C99中,您可以声明一个可变长度数组(VLA),即使有多个维度,如果这样做,您可以直接在数组上使用sizeof运算符,这可以更方便。但是对VLA有很多限制;它们往往不像你期望的那样工作。但是,以下工作正常:
double m[rows][cols];
memset(m, 0, sizeof m);
Note that in C99, unlike traditional C or C++, the compiled sizeof
operator in this case may actually create run-time code, and therefore violates the expectation of many programmers that sizeof
does not evaluate its argument.
请注意,在C99中,与传统的C或C ++不同,在这种情况下,编译后的sizeof运算符实际上可能会创建运行时代码,因此违反了许多程序员的期望,即sizeof不会评估其参数。
#1
10
The answer that works in C and C++ is dynamic memory allocation
在C和C ++中有效的答案是动态内存分配
int *arr = (int*)malloc(size*sizeof(int));
In C++ you would prefer to use new instead of malloc, but the principle is the same.
在C ++中,您更喜欢使用new而不是malloc,但原理是相同的。
int* arr = new int[size];
#2
1
The C language does not give the option to initialize an array if his size is not an constant value
如果数组的大小不是常量,则C语言不提供初始化数组的选项
In C99 you can use a variable length array and then initialize it by using a loop.
在C99中,您可以使用可变长度数组,然后使用循环对其进行初始化。
if I initialize it generally (int *arr;) so it gives me error of 'arr' is not being initialized.
如果我一般初始化它(int * arr;)所以它给我错误'arr'没有被初始化。
This is because a pointer must be initialized (points to a pointee - excluding NULL) before it is being used in program.
这是因为在程序中使用指针之前必须初始化指针(指向指针对象 - 不包括NULL)。
#3
0
In C, you can initialize objects to 0 with memset
. It's not possible to use memset
to portably initialize objects other than character arrays to a repeated non-zero value.
在C中,您可以使用memset将对象初始化为0。不可能使用memset将字符数组以外的对象可移植地初始化为重复的非零值。
In C++, the same is true, but it is restricted to so-called "POD" objects (Plain Old Data), which are basically the same objects you could have in C (no virtual functions, no private data members, etc. -- the precise definition is in the standard). It's not good C++ style, but it's possible.
在C ++中,情况也是如此,但它仅限于所谓的“POD”对象(普通旧数据),它们与C中的对象基本相同(没有虚函数,没有私有数据成员等等 - - 精确定义在标准中)。它不是很好的C ++风格,但它是可能的。
In both C and C++ you can find the total size of an array in bytes (which you need to pass to memset
) by multiplying the dimensions and the size of a single data element. For example:
在C和C ++中,您可以通过将单个数据元素的维度和大小相乘来找到数组的总大小(以字节为单位)(需要传递给memset)。例如:
void InitializeMatrix(double *m, size_t rows, size_t cols) {
memset(m, 0, rows * cols * sizeof *m);
}
In C99, you can declare a variable length array (VLA), even with multiple dimensions, and if you do so, you can use the sizeof
operator directly on the array, which can be a lot more convenient. But there are lots of restrictions on VLAs; they often don't work the way you expect them to. However, the following does work:
在C99中,您可以声明一个可变长度数组(VLA),即使有多个维度,如果这样做,您可以直接在数组上使用sizeof运算符,这可以更方便。但是对VLA有很多限制;它们往往不像你期望的那样工作。但是,以下工作正常:
double m[rows][cols];
memset(m, 0, sizeof m);
Note that in C99, unlike traditional C or C++, the compiled sizeof
operator in this case may actually create run-time code, and therefore violates the expectation of many programmers that sizeof
does not evaluate its argument.
请注意,在C99中,与传统的C或C ++不同,在这种情况下,编译后的sizeof运算符实际上可能会创建运行时代码,因此违反了许多程序员的期望,即sizeof不会评估其参数。