qq_45067123
qq_45067123
采纳率100%
2020-05-01 19:20 阅读 157

学习C语言数组中遇到的问题,请大佬们帮忙解答,非常感谢

5

#include

#define ROWS 2
#define COLS 2

void copy_1(int rows, int cols, double * so, double * ta);

int main()
{
int i, j;

double source[ROWS][COLS] = { { 1.1, 2.2 }, { 3.3, 4.4 } };
double target[ROWS][COLS];

for (i = 0; i < ROWS; i++)
{
    for (j = 0; j < COLS; j++)
        printf("%.2ld   ", source[i][j]);

    printf("\n");
}

copy_1(ROWS, COLS, source, target);

for (i = 0; i < ROWS; i++)
{
    for (j = 0; j < COLS; j++)
        printf("%.2ld   ", target[i][j]);

    printf("\n");
}

return 0;

}

void copy_1(int rows, int cols, double * so, double * ta)
{
int r, c;

for (r = 0; r < rows; r++)
{
for (c = 0; c < cols; c++)
((ta + r) + c) = ((so + r) + c);
}
}
//上述代码的功能是将一个二维数组中的数据拷贝到另一个二维数组中,但是出现了错误,错误提示如下:E:\CC++\c file\CODE-C\free2\main.c|43|error: invalid type argument of unary '*' (have 'double')|

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2020-05-01 22:01
    #include <stdio.h>
    
    #define ROWS 2
    #define COLS 2
    void copy_1(int rows, int cols, double * so, double * ta);
    int main()
    {
        int i, j;
        double source[ROWS][COLS] = { { 1.1, 2.2 }, { 3.3, 4.4 } };
        double target[ROWS][COLS];
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
                printf("%.2lf ", source[i][j]);
            printf("\n");
        }
        copy_1(ROWS, COLS, (double *)source, (double *)target);
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
                printf("%.2lf ", target[i][j]);
            printf("\n");
        }
        return 0;
    }
    void copy_1(int rows, int cols, double * so, double * ta)
    {
        int r, c;
        for (r = 0; r < rows; r++)
        {
            for (c = 0; c < cols; c++)
                *(ta + r * cols + c) = *(so + r * cols + c);
        }
    }
    
    

    图片说明

    如果问题得到解决,请点下采纳

    点赞 2 评论 复制链接分享
  • phenix2009 白色一大坨 2020-05-01 22:17

    直接贴代码

    #include <stdio.h>
    
    #define ROWS 2
    #define COLS 2
    
    void copy_1(int rows, int cols, double (*so)[COLS], double (*ta)[COLS]);
    
    int main()
    {
        int i, j;
    
        double source[ROWS][COLS] = { { 1.1, 2.2 }, { 3.3, 4.4 } };
        double target[ROWS][COLS];
    
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
                printf("%.2lf   ", source[i][j]);
    
            printf("\n");
        }
    
        copy_1(ROWS, COLS, source, target);
    
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
                printf("%.2lf   ", target[i][j]);
    
            printf("\n");
        }
    
        return 0;
    
    }
    
    void copy_1(int rows, int cols, double (*so)[COLS], double (*ta)[COLS])
    {
        int r, c;
    
        for (r = 0; r < rows; r++)
        {
            for (c = 0; c < cols; c++)
                ta[r][c] = so[r][c];
        }
    }
    
    点赞 1 评论 复制链接分享

相关推荐