指向数组的指针和指针数组的内存分配

3

以下是我卡住的代码部分。我想要为以下内容动态分配内存:

  • 指向数组的指针
  • 指针数组

我遇到了一些错误,比如从int *到int的无效转换等等。

指向数组的指针

int (*array)[nrows][ncolumns];
array = (int*)malloc(nrows * ncolumns * sizeof(int));


printf("\n Enter the elements:\n");

for(i=0; i<nrows; i++)
{
    for(j=0; j<ncolumns; j++)
    {
        scanf("%d", array[i][j]);   
    }
}

printf("Entered array is :\n\n");

for(i = 0;i<nrows; i++)
{
    for(j = 0; j<ncolumns; j++)
    {
        if(j== ncolumns-1)
        {
            printf("%d \n", *array[i][j]);
        }
        else
        {
            printf("%d", *array[i][j]);
        }

指针数组

int *array[nrows][ncolumns];
array[nrows][ncolumns] = (int*)malloc(nrows * ncolumns * sizeof(int));

printf("Enter elements:\n");

for(i = 0; i<nrows; i++)
{
    for(j = 0; j<ncolumns;j++)
    {
        scanf("%d",&array[i][j]);


    }
}


printf("Entered array is: \n");
for(i = 0; i<nrows; i++)
{
    for(j = 0; j<ncolumns;j++)
    {
        if(j == ncolumns-1)
        {
            printf("%d \n",array[i][j]);
        }
        else
        {
            printf("%d \t",array[i][j]);
        }


    }
}

2
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - molbdnilo
谢谢。但实际上我没有那么多时间。我必须在1小时内纠正它。请帮助我。内存分配部分正确吗? - SKD
2
@Sujit 嗯,你应该早点开始学习。提示:在1)中,array[i]是一个二维数组,array[i][j]是一个一维数组。在2)中,array[i][j]是一个(未初始化的)指针,而不是int - molbdnilo
1
@Sujit array[nrows][ncolumns] 是数组之外的元素;对其进行赋值是未定义的。 - molbdnilo
1
@Sujit,你还需要修复scanf和printf的使用中的错误,应该是scanf(“%d”,&array [i] [j]);printf(“%d”,array [i] [j]); - M.M
显示剩余6条评论
1个回答

4

1> 数组指针

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

int main(void){
    int nrows    = 3;
    int ncolumns = 4;
    int i, j;

    int (*array)[nrows][ncolumns];//do you want <<int (*array)[ncolumns]>> ?
    //like as int src[nrows][ncolumns]; array = &src;
    array = malloc(nrows * ncolumns * sizeof(int));//(int*) : type mismatch

    printf("\nEnter the elements:\n");

    for(i = 0; i<nrows; i++){
        for(j = 0; j<ncolumns; j++){
            scanf("%d", &(*array)[i][j]);
        }
    }

    printf("Entered array is :\n\n");

    for(i = 0; i<nrows; i++){
        for(j = 0; j<ncolumns; j++){
            if(j != 0)
                putchar(' ');
            printf("%d", (*array)[i][j]);//need ( )
        }
        putchar('\n');
    }
    free(array);
    return 0;
}

2>指针数组

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

int main(void){
    int nrows   = 3;
    int ncolumns = 4;
    int i, j;

    int *array[nrows][ncolumns];
    int *src = (int*)malloc(nrows * ncolumns * sizeof(int));//no need (int*)

    printf("Enter elements:\n");

    for(i = 0; i<nrows; i++){
        for(j = 0; j<ncolumns;j++){
            array[i][j] = &src[ i * ncolumns + j];//pointer pointed to entity (src[ i * ncolumns + j])
            scanf("%d", array[i][j]);//type of array[i][j] is int *
        }
    }

    printf("Entered array is: \n");
    for(i = 0; i<nrows; i++){
        for(j = 0; j<ncolumns; j++){
            if(j != 0)
                putchar(' ');
            printf("%d", *array[i][j]);//need * for dereference
        }
        putchar('\n');
    }

    free(src);
    return 0;
}

3> 选项

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

int main(void){
    int nrows    = 3;
    int ncolumns = 4;
    int i, j;

    int (*array)[ncolumns];
    array = (int (*)[ncolumns])malloc(nrows * sizeof(*array));//sizeof(*array) : sizeof(int[ncolumns])

    printf("\nEnter the elements:\n");

    for(i = 0; i<nrows; i++){
        for(j = 0; j<ncolumns; j++){
            scanf("%d", &array[i][j]);
        }
    }

    printf("Entered array is :\n\n");

    for(i = 0; i<nrows; i++){
        for(j = 0; j<ncolumns; j++){
            if(j != 0)
                putchar(' ');
            printf("%d", array[i][j]);
        }
        putchar('\n');
    }
    free(array);
    return 0;
}

非常感谢,这真的很有帮助。不过,在第一个程序中,我仍然遇到了“类型转换”错误。 - SKD
1
@Sujit 也许你正在使用 C++ 编译器作为 C 编译器。(在 C 中不需要从 void * 进行转换),尝试使用 array = (int (*)[nrows][ncolumns])malloc(nrows * ncolumns * sizeof(int)); - BLUEPIXY
我正在使用Dev-C++编译器。再次感谢。现在两个程序都运行良好。 - SKD
抱歉,我没有注意到你的编辑。第三个选项也可以工作。(虽然它让我很困惑)。我有一个问题,为什么我们需要在malloc之前进行类型转换? - SKD
为什么不直接这样做呢?它没有改变,以避免不必要的混淆。(在选项中执行它) - BLUEPIXY
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接