C语言 初始化 C 动态数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17937623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing C dynamic arrays
提问by EnterKEY
How do I initialize a dynamic array allocated with malloc? Can I do this:
如何初始化使用 malloc 分配的动态数组?我可以这样做吗:
int *p;
p = malloc(3 * sizeof(*p));
p = {0, 1, 2};
...
free(p);
Or do I need to do something like this:
或者我需要做这样的事情:
int *p, x;
p = malloc(3 * sizeof(*p));
for (x = 0; x < 3; x++)
p[x] = 0;
...
free(p);
Or is there a better way to do it?
或者有没有更好的方法来做到这一点?
回答by phoxis
You need to allocate a block of memory and use it as an array as:
您需要分配一块内存并将其用作数组,如下所示:
int *arr = malloc (sizeof (int) * n); /* n is the length of the array */
int i;
for (i=0; i<n; i++)
{
arr[i] = 0;
}
If you need to initialize the array with zeros you can also use the memsetfunction from C standard library (declared in string.h).
如果您需要用零初始化数组,您还可以使用memsetC 标准库中的函数(在 中声明string.h)。
memset (arr, 0, sizeof (int) * n);
Here 0is the constant with which every locatoin of the array will be set. Note that the last argument is the number of bytes to be set the the constant. Because each location of the array stores an integer therefore we need to pass the total number of bytes as this parameter.
这0是用于设置数组的每个位置的常量。请注意,最后一个参数是要设置常量的字节数。由于数组的每个位置都存储一个整数,因此我们需要将总字节数作为此参数传递。
Also if you want to clear the array to zeros, then you may want to use callocinstead of malloc. callocwill return the memory block after setting the allocated byte locations to zero.
此外,如果您想将数组清零,那么您可能需要使用calloc代替malloc. calloc将分配的字节位置设置为零后返回内存块。
After you have finished, free the memory block free (arr).
完成后,释放内存块free (arr)。
EDIT1
编辑1
Note that if you want to assign a particular integer in locations of an integer array using memsetthen it will be a problem. This is because memsetwill interpret the array as a byte array and assign the byte you have given, to every byte of the array. So if you want to store say 11243 in each location then it will not be possible.
请注意,如果您想在整数数组的位置分配一个特定的整数,memset那么这将是一个问题。这是因为memset将数组解释为字节数组并将您提供的字节分配给数组的每个字节。因此,如果您想在每个位置存储 11243,那么这是不可能的。
EDIT2
编辑2
Also note why every time setting an int array to 0 with memsetmay not work: Why does "memset(arr, -1, sizeof(arr)/sizeof(int))" not clear an integer array to -1?as pointed out by @Shafik Yaghmour
还要注意为什么每次将 int 数组设置为 0 时memset可能不起作用:为什么“memset(arr, -1, sizeof(arr)/sizeof(int))”不将整数数组清除为 -1?正如@Shafik Yaghmour 指出的那样
回答by EnterKEY
Instead of using
而不是使用
int * p;
p = {1,2,3};
we can use
我们可以用
int * p;
p =(int[3]){1,2,3};
回答by caf
You cannot use the syntax you have suggested. If you have a C99 compiler, though, you can do this:
您不能使用您建议的语法。但是,如果您有 C99 编译器,则可以执行以下操作:
int *p;
p = malloc(3 * sizeof p[0]);
memcpy(p, (int []){ 0, 1, 2 }, 3 * sizeof p[0]);
If your compiler does not support C99 compound literals, you need to use a named template to copy from:
如果您的编译器不支持 C99 复合文字,您需要使用命名模板来复制:
int *p;
p = malloc(3 * sizeof p[0]);
{
static const int p_init[] = { 0, 1, 2 };
memcpy(p, p_init, 3 * sizeof p[0]);
}
回答by Lidong Guo
p = {1,2,3} is wrong.
p = {1,2,3} 是错误的。
You can never use this:
你永远不能使用这个:
int * p;
p = {1,2,3};
loop is right
循环是对的
int *p,i;
p = malloc(3*sizeof(int));
for(i = 0; i<3; ++i)
p[i] = i;
回答by user763410
I think the more tedious way is the only way to do it. I tried the first one and it doesn't compile (After commenting the '...')
我认为更乏味的方法是唯一的方法。我尝试了第一个,但它无法编译(在评论“...”之后)
No many shortcuts in 'C' I guess.ac
'C' 中没有很多快捷方式,我猜.ac

