简体   繁体   English

如何获取多维数组的输入并使用指针在C中打印数组?

[英]How to take input for multidimensional array and print the array in C using pointers?

I am trying to take integer data and store it into a dimensional array but i am unable to do it. 我正在尝试获取整数数据并将其存储到维数组中,但是我无法做到这一点。 Somebody help me please.. 请有人帮我..

I tried using *(*(arr+i) + j) where arr is a pointer to the 2-D array , i and j are the loop variables, I get an error 我尝试使用*(*(arr+i) + j) ,其中arr是指向二维数组的指针, ij是循环变量,但出现错误

error: invalid type argument of unary '*' (have 'int') scanf("%d", ( (arr+i) + j)); 错误:一元'*'(具有'int')scanf(“%d”, (arr + i)+ j))的无效类型参数;

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

int main(){
        int n,*arr,i,j,k;
        scanf("%d",&n);
        arr = malloc(n*n*sizeof(int));
        memset(arr,0,n*n*sizeof(int));
        for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                        scanf("%d", *(*(arr+i) + j));
                }
        }
        for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                        printf("%d ", *(*(arr+i) + j);
                }
                printf("\n");
        }

}

My input was: 我的输入是:

3
11 2 4
4 5 6
10 8 -12

int* arr ... arr = malloc(n*n*sizeof(int)); int* arr ... arr = malloc(n*n*sizeof(int)); gives you a "mangled" 2D array - it's actually a 1D array. 给您一个“混杂”的2D数组-实际上是一个1D数组。 Meaning you'll have to access it as arr[i*n + j] . 意味着您必须以arr[i*n + j]身份对其进行访问。

Mangled arrays are mostly a thing of the past though. 错综复杂的数组已成为过去。 With modern standard C, you can replace the whole code with this: 使用现代标准C,您可以使用以下代码替换整个代码:

int (*arr)[n] = malloc( sizeof(int[n][n]) );
...
for(size_t i=0; i<n; i++)
  for(size_t j=0; j<n; j++)
    scanf("%d", &arr[i][j]);
...
free(arr);

Also note, if you need to zero-initialize the whole array to zero, you are better off using calloc since it does just that. 还要注意,如果您需要将整个数组归零初始化为零,那么最好使用calloc因为它只是这样做。

The type of the variable arr is int * . 变量arr的类型为int *

int n,*arr,i,j,k;
      ^^^^

So there is no a two-dimensional array in your program. 因此,您的程序中没有二维数组。

So for example this expression 所以例如这个表达

*(arr+i) + j

has type int . 具有类型int And this expression 而这个表达

*(*(arr+i) + j)

tries to dereference an object of the type int that is an object that is not a pointer. 尝试取消引用类型为int的对象,该对象是不是指针的对象。

If your compiler supports variable length arrays then the program can look like 如果您的编译器支持可变长度数组,则程序看起来像

#include <stdio.h>

int main(void) 
{
    size_t n;

    scanf( "%zu", &n );

    int a[n][n];

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            scanf( "%d", *( a + i ) + j );
        }
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            printf( "%3d ", *( *( a + i ) + j ) );
        }
        putchar( '\n' );
    }

    return 0;
}

Its output is 它的输出是

 11   2   4 
  4   5   6 
 10   8 -12 

Otherwise another approach is dynamically to allocate a one-dimensional array of pointers and then correspondingly one-dimensional arrays of integers. 否则,另一种方法是动态分配指针的一维数组,然后分配相应的整数一维数组。

For example 例如

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

int main(void) 
{
    size_t n;

    scanf( "%zu", &n );

    int **a = malloc( n * sizeof( int * ) );

    for ( size_t i = 0; i < n; i++ )
    {
        *( a + i ) = malloc( n * sizeof( int ) );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            scanf( "%d", *( a + i ) + j );
        }
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            printf( "%3d ", *( *( a + i ) + j ) );
        }
        putchar( '\n' );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        free( *( a + i ) );
    }

    free( a );

    return 0;
}

The program output will be the same as shown above. 程序输出将与上面显示的相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM