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

时间:2015-09-14 09:08:30

标签: c arrays pointers

以下是我坚持使用的代码部分。我想为

动态分配内存
  • 指向数组的指针
  • 指针数组

我收到一些错误消息,例如从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]);
        }


    }
}

1 个答案:

答案 0 :(得分:4)

1&GT;指向数组的指针

#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&GT;选项

#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;
}