使用malloc()动态创建多维(2D)数组?

使用malloc()动态创建多维(2D)数组?,c,arrays,C,Arrays,如果有人用malloc函数告诉我这段代码有什么问题,我不知道该怎么办 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <process.h> int main() { int row; int column; int j=0; int **ptr; printf("Number of rows: \n"); scanf ("%d",&

如果有人用malloc函数告诉我这段代码有什么问题,我不知道该怎么办

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

 int main()
 {
 int row;
 int column;
 int j=0;
 int **ptr;

 printf("Number of rows: \n");
 scanf ("%d",&row);
 printf("Number of columns: \n"); 
 scanf ("%d",&column);  
#包括
#包括
#包括
#包括
int main()
{
int行;
int列;
int j=0;
int**ptr;
printf(“行数:\n”);
scanf(“%d”行和第行);
printf(“列数:\n”);
scanf(“%d”列和列);
有一个错误,??如果有人知道怎么写,请

 int* ptr = malloc( row * sizeof(*ptr) ); /*Allocate integer pointers for the rows */
    if(ptr != NULL)
    {
        for(int m = 0; m < row; m++) /* Loop through each row pointer to allocate     memory for columns*/
        {
            /* Set p[i] pointer to a block of memory for 'column' number of integers */
            ptr[m] = malloc(column * sizeof **ptr); /*Here, sizeof(**p) is same as sizeof(int) */
            if(ptr[m] == NULL)
            {
                printf("Memory allocation failed. Exiting....");
                 exit(1);  
            }
        }

    }
    else
    {
        printf("Memory allocation failed. Exiting....");
         exit(1);  
    }





for(int i=0; i<row; i++)
{
   for(int j=0; j<column; j++)
{
printf("Enter values [%d] [%d]: \n",i+1,j+1 );  
scanf ("%d",&ptr[i][j]);
}
}


free (ptr); 
system("PAUSE");    
return 0;
}
int*ptr=malloc(row*sizeof(*ptr));/*为行分配整数指针*/
如果(ptr!=NULL)
{
for(int m=0;m对于(int i=0;i而不是使用
sizeof(ptr)
sizeof**ptr
,尝试在第一个malloc中使用
sizeof(int*)
,在第二个malloc中使用
sizeof(int)

int* ptr = malloc( row * sizeof(*ptr) );
…为了这个

ptr = malloc( row * sizeof(*ptr) );
您已经在此处声明了
ptr

int **ptr;

首先,您的代码非常混乱。您的malloc调用也应该如下所示,例如:

   int** ptr = (int**)malloc(row * sizeof(ptr*));
指针的大小始终是4(几乎所有时间,除非您使用的是1982年生产的计算机),因为它是一个内存地址,也称为一个整数,它是4个字节。在for循环中,它应该读取如下内容:

    ptr[m] = (int*)malloc(column * sizeof(ptr));

如果您觉得需要使用<代码> Malc C/代码>(和<代码>免费/代码>)进行动态分配,那么这里已经有了一些很好的答案。但是请考虑在您的示例中根本不需要使用<代码> MalOC 。 您可以简单地声明如下所示的可变大小的自动数组。您的代码将非常清晰,潜在的错误将减少。此功能在C语言中自ISO C99(早期,在某些编译器中)起就可用

如果数组非常大,则可能会有一个不使用可变大小数组声明的论点。您的示例是为每个数组元素获取用户输入,因此这表明您没有分配数百兆字节或任何会炸毁堆栈的内容!因此这对您来说很好。祝您好运

#include <stdio.h>

int main()
{
    int rows;
    int columns;

    printf("Number rows: ");
    scanf ("%d", &rows);
    printf("Stevilo columns: "); 
    scanf ("%d", &columns);  

    int values[rows][columns];   /* <-- no need to malloc(), no need to free() */

    for(int i=0; i<rows; i++) {
        for(int j=0; j<columns; j++) {
            printf("Enter value[%d][%d]: ",i+1,j+1 );  
            scanf("%d", &values[i][j]);
        }
    }
}
#包括
int main()
{
int行;
int列;
printf(“行数:”);
scanf(“%d”,行和行);
printf(“Stevilo列:”);
scanf(“%d”列和列);

int values[rows][columns];/*感谢大家这么快的回答,它非常有用,现在它可以正常工作了,没有任何错误

我这样做了:

    int** ptr = (int**) malloc(row * sizeof(int)); /*Allocate integer pointers for the rows */
    if(ptr != NULL)
    {
        for(int m = 0; m < row; m++) /* Loop through each row pointer to allocate memory for columns*/
        {
            /* Set p[i] pointer to a block of memory for 'column' number of integers */
            ptr[m] = (int*)malloc(column * sizeof(int)); /*Here, sizeof(**p) is same as sizeof(int) */
            if(ptr[m] == NULL)
            {
                printf("Memory allocation failed. Exiting....");
                 exit(1);  
            }
        }

    }
    else
    {
        printf("Memory allocation failed. Exiting....");
         exit(1);  
    }
int**ptr=(int**)malloc(row*sizeof(int));/*为行分配整数指针*/
如果(ptr!=NULL)
{
for(int m=0;m
从初始alloc中删除
int*ptr
。您已经在前面的声明中将其声明为
int**ptr;
。这是:
int*ptr=malloc(row*sizeof(*ptr));
应该是:
ptr=malloc(row*sizeof(*ptr));
op的大小调整是正确的。另外,
sizeof(*int)
甚至不是有效的C代码。
sizeof(*ptr)
sizeof(**ptr)
sizeof(int*)
sizeof(int)相同,在这种情况下,请不要教从“代码> MaloC/<代码>中返回。这是C而不是C++。原来的代码在那一点上是正确的,现在你错了。<代码> sieZof(PTR*} /Code >是不正确的语法;应该是代码> siZeof pTR 。而且,在分配给<代码> PTR[M] < /Code >,<代码> sieof(PTR)。
不正确;它应该是
sizeof*ptr[m]一个指针的大小既不是总是四字节也不是总是一个<代码> int <代码>的大小。@ JensGustedt如果铸造MALLC是错误的,那么为什么我看到的例子几乎每一个MALOC的例子?到底是什么错误?“KATANIE”,可能是因为你正在看C++写的例子,而不是为C.C的常见问题解答的顶部就是您要查找的:
int**ptr=(int**)malloc(row*sizeof(int));
使用了错误的大小。它应该是
row*sizeof*ptr