【问题标题】:function and array in c program using pointerc程序中使用指针的函数和数组
【发布时间】:2021-03-17 08:53:53
【问题描述】:

我有程序代码,但输出是正确的。

输出应该:

1 2
3 4

输入是:

1 2 3 4

这是我的代码:

#include<stdio.h>
#define size 100

int array2d(int *x[size][size],int a,int b){
    int i,j;
    for(i=0;i<a;i++){
        for(j=0;j<b;j++){
            printf("%d ", x[i][j]); x[i][j]++;
        }
        printf("\n");
    }
}
int main(){
    int a,b;
    int x[size][size];
    
    printf("enter the size of array (row) & (column) : ");
    scanf("%d %d",&a,&b);
    
    printf("enter the number : ");
    scanf("%d",&x[size][size]);
    
    array2d(x,a,b);
    
    return 0;
}

我的代码显示输出:

0 0
0 0

我应该怎么做才能修复它?也许有人想帮我修复它。

【问题讨论】:

    标签: c dev-c++


    【解决方案1】:

    您没有从控制台正确读取元素。您读取输入的方式是错误的。

    #include <stdio.h>
    #define size 100
    
    int array2d(int x[size][size], int a, int b)
    {
        int i, j;
        for (i = 0; i < a; i++)
        {
            for (j = 0; j < b; j++)
            {
                printf("%d ", x[i][j]);
            }
            printf("\n");
        }
    }
    int main()
    {
        int a, b;
        int x[size][size];
    
        printf("enter the size of array (row) & (column) : ");
        scanf("%d %d", &a, &b);
    
        printf("enter the number : ");
    
        for (int i = 0; i < a; i++) //reading input
        {
            for (int j = 0; j < b; j++)
            {
                scanf("%d", &x[i][j]);
            }
        }
    
        array2d(x, a, b);
    
        return 0;
    }
    

    输出:

    enter the size of array (row) & (column) : 2 2
    enter the number : 1 2 3 4
    1 2 
    3 4
    

    【讨论】:

    • 我应该怎么做才能在那里输入指针? @克里希纳
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多