Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:在main中有一个指向指针的指针,并在其他函数中创建一个数组_C_Arrays_Function_Pointers - Fatal编程技术网

C:在main中有一个指向指针的指针,并在其他函数中创建一个数组

C:在main中有一个指向指针的指针,并在其他函数中创建一个数组,c,arrays,function,pointers,C,Arrays,Function,Pointers,我有如下代码: #include <stdio.h> #include <stdlib.h> void mad(int ***, int, in); int main(void) { int **x; int n,m; scanf("%d%d",&n,&m); mad(x,n,m); x[0][0] = 5; printf("%d\n",x[0][0]); return 0; } void

我有如下代码:

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

void mad(int ***, int, in);

int main(void) {
    int **x;
    int n,m;

    scanf("%d%d",&n,&m);
    mad(x,n,m);
    x[0][0] = 5;
    printf("%d\n",x[0][0]);

    return 0;
}

void mad(int ***x, int n, int m) {
    int i;
    **x = malloc(sizeof(int *));

    for (i = 0; i < n; i++)
        ***(x + i) = malloc(m * sizeof(int));
}
#包括
#包括
无效mad(int***,int,in);
内部主(空){
整数**x;
int n,m;
scanf(“%d%d”,&n,&m);
mad(x,n,m);
x[0][0]=5;
printf(“%d\n”,x[0][0]);
返回0;
}
无效mad(整数***x,整数n,整数m){
int i;
**x=malloc(sizeof(int*);
对于(i=0;i

这是错误的。有人能解释为什么这是错误的,并帮助我纠正它。

*x
是指向数组的指针<代码>malloc(n*sizeof(int*)分配指针数组
*(*x+i)
(*x)[i]
是一个数组<代码>malloc(m*sizeof(int))分配一个数组。调整您的代码,如下所示:

void mad(int ***x, int n, int m){
    int i;

    *x = malloc(n * sizeof(int*));       // allocate memory where x refers to
    for( i=0; i<n; i++ )
      *(*x+i) = malloc(m * sizeof(int)); // similar to (*x)[i] = malloc(m * sizeof(int))
}

mad(&x,n,m);
 // ^
void-mad(整数***x,整数n,整数m){
int i;
*x=malloc(n*sizeof(int*);//在x所指的位置分配内存

对于(i=0;i您需要有一个指向指针的变量,因此声明如下:

int **x;
但是,如果希望另一个函数为其赋值,则需要传递指向该变量的指针,因此将函数声明为

void mad(int ***x,int n, int m);
通过向变量传递指针来调用它:

mad(&x,m,n);
并为取消引用的指针指定一个新值

void mad(int ***x,int n, int m)
{
    *x = malloc(...);
}
顺便说一句,第一个
malloc
调用似乎不正确–您分配的内存块足够大,可以保留一个指向
int
的指针,而它可能应该是指向
int
n
指针:

    *x = malloc(n*sizeof(int *));
然后分配
n
数组的行,每个行的长度为m
int
s:

    for (i=0; i<n; i++)
        *(*x + i) = malloc(m*sizeof(int));

对于(i=0;i您应该避免所有那些效率低下的基于指针的查找表。它们不是数组,因为它们没有分配到相邻的内存单元中

改为分配真实的二维阵列:

void alloc2d ( size_t x, size_t y, int(**ptr)[x][y] )
{
  *ptr = malloc ( sizeof(int[x][y]) );
}

int main (void)
{
  const int x = 5;
  const int y = 3;
  int (*arr_ptr)[x][y];

  alloc2d(x, y, &arr_ptr);

  int (*array2d)[y] = *arr_ptr; // point at 1st element which is an array
  array2d[i][j] = something;

  ...
  free(arr_ptr);
}

**x=malloc(sizeof(int*);
-->
**x=malloc(sizeof(int*))*n)是的,我在malloc中忘记了n,但也不起作用。你是说它不编译吗?如果有错误,你应该在帖子中提供。函数的原型和函数声明不相等,它们应该相等。编译器没有给你任何错误或警告吗?>gcc-o end.c end.c:在函数“main”中:end、 c:10:9:警告:从不兼容的指针类型[enabled by default]end传递'mad'的参数1。c:4:6:注意:应为'int***',但参数的类型为'int**'end。c:在函数'mad'中:end。c:20:37:警告:赋值使指针中的整数不带强制转换[enabled by default]“
*x
是二维数组”这不是2D数组,而是一个基于指针的分段查找表。