This question already has an answer here:
这个问题在这里已有答案:
- casting void** to 2D array of int - C 1 answer
- 将void **转换为int - C 1答案的2D数组
Something like this:
像这样的东西:
struct s
{
int a;
int b;
}
void f(struct s **a)
{
a[0][0].a = 0; // Access violation here
}
int main()
{
struct s a[5][3];
f(a);
return(0);
}
So how can I access content of a inside function f using 2D array notation?
那么如何使用2D数组表示法访问内部函数f的内容?
1 个解决方案
#1
2
An array like a[5][3]
stores the struct
instances contigously, while struct s **a
will store pointers contigously such that each pointer points to one instance of struct s
. So struct s a[5][3]
(which is automaticaly converted to a pointer) and struct s **a
are incompatible pointers, if you compile with warnings you would know that.
像[5] [3]这样的数组存储结构实例,而struct s ** a将重复存储指针,使每个指针指向struct的一个实例。所以struct s a [5] [3](自动转换为指针)和struct s ** a是不兼容的指针,如果你使用警告进行编译就会知道。
A simple solution would be
一个简单的解决方案就是
void f(struct s a[][3])
{
a[0][0].a = 0; // Access violation here
}
A better solution, is
更好的解决方案是
#include <stdlib.h>
struct some_structure
{
int value1;
int value2;
};
void
set_value(struct some_structure **array, size_t row, size_t column)
{
array[row][column].value1 = 0;
array[row][column].value2 = 0;
}
int
main(void)
{
struct some_structure **array;
array = malloc(5 * sizeof(*array));
if (array == NULL)
return -1; // Allocation Failure
for (size_t i = 0 ; i < 5 ; ++i)
{
array[i] = malloc(sizeof(*(array[i])));
if (array[i] == NULL)
return -1; // Allocation Failure
}
set_value(array, 0, 0);
for (size_t i = 0 ; i < 5 ; ++i)
free(array[i]);
free(array);
return 0;
}
As you see when I say above will store pointers it is because you need to allocate memory for that, you can by using malloc()
like the example above.
正如你所说,当我说上面会存储指针时,因为你需要为此分配内存,你可以像上面的例子一样使用malloc()。
#1
2
An array like a[5][3]
stores the struct
instances contigously, while struct s **a
will store pointers contigously such that each pointer points to one instance of struct s
. So struct s a[5][3]
(which is automaticaly converted to a pointer) and struct s **a
are incompatible pointers, if you compile with warnings you would know that.
像[5] [3]这样的数组存储结构实例,而struct s ** a将重复存储指针,使每个指针指向struct的一个实例。所以struct s a [5] [3](自动转换为指针)和struct s ** a是不兼容的指针,如果你使用警告进行编译就会知道。
A simple solution would be
一个简单的解决方案就是
void f(struct s a[][3])
{
a[0][0].a = 0; // Access violation here
}
A better solution, is
更好的解决方案是
#include <stdlib.h>
struct some_structure
{
int value1;
int value2;
};
void
set_value(struct some_structure **array, size_t row, size_t column)
{
array[row][column].value1 = 0;
array[row][column].value2 = 0;
}
int
main(void)
{
struct some_structure **array;
array = malloc(5 * sizeof(*array));
if (array == NULL)
return -1; // Allocation Failure
for (size_t i = 0 ; i < 5 ; ++i)
{
array[i] = malloc(sizeof(*(array[i])));
if (array[i] == NULL)
return -1; // Allocation Failure
}
set_value(array, 0, 0);
for (size_t i = 0 ; i < 5 ; ++i)
free(array[i]);
free(array);
return 0;
}
As you see when I say above will store pointers it is because you need to allocate memory for that, you can by using malloc()
like the example above.
正如你所说,当我说上面会存储指针时,因为你需要为此分配内存,你可以像上面的例子一样使用malloc()。