c语言变长数组拷贝问题?

软件和网站开发以及相关技术探讨
回复
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

c语言变长数组拷贝问题?

#1

帖子 drongh » 2014-12-12 10:11

题目是这样的,把一个2维数组拷贝给另一个变长数组,并显示。。

代码: 全选

#include "stdio.h"

void copy_array(int m, int n, double source[m][n], double target[m][n]);
void display_array(int m, int n, double arr[m][n]);

int main(int argc, char const *argv[])
{	
	int m = 3;
	int n = 5;
	double source[m][n] = {
		{1, 2, 3, 4, 5},
		{6, 7, 8, 9, 10},
		{11, 12, 13, 14, 15}
	};

	double target[m][n];
	copy_array(m, n, source, target);
	printf("array target is:\n");
	display_array(m, n, target);
	printf("\n");
	return 0;
}

void display_array(int m, int n, double arr[m][n])
{
	int row;
	int col;
	for (row = 0; row < m; row++)
	{
		for (col = 0; col < n; col++)
		{
			printf("%3.1f", arr[row][col]);
		}
		printf("\n");
	}
}

void copy_array(int m, int n, double source[m][n], double target[m][n])
{
	for (int row = 0; row < m; row++)
	{
		for (int col = 0; col < n; col++)
		{
			target[row][col] = source[row][col];
		}
	}
}
下面是出错信息:
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:3:46: error: use of parameter outside function body before ‘]’ token
void copy_array(int m, int n, double source[m][n], double target[m][n]);
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:3:49: error: use of parameter outside function body before ‘]’ token
void copy_array(int m, int n, double source[m][n], double target[m][n]);
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:3:50: error: expected ‘)’ before ‘,’ token
void copy_array(int m, int n, double source[m][n], double target[m][n]);
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:3:52: error: expected unqualified-id before ‘double’
void copy_array(int m, int n, double source[m][n], double target[m][n]);
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:4:46: error: use of parameter outside function body before ‘]’ token
void display_array(int m, int n, double arr[m][n]);
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:4:49: error: use of parameter outside function body before ‘]’ token
void display_array(int m, int n, double arr[m][n]);
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:24:46: error: use of parameter outside function body before ‘]’ token
void display_array(int m, int n, double arr[m][n])
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:24:49: error: use of parameter outside function body before ‘]’ token
void display_array(int m, int n, double arr[m][n])
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c: In function ‘void display_array(...)’:
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:28:22: error: ‘m’ was not declared in this scope
for (row = 0; row < m; row++)
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:30:23: error: ‘n’ was not declared in this scope
for (col = 0; col < n; col++)
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:32:20: error: ‘arr’ was not declared in this scope
printf("%3.1f", arr[row][col]);
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c: At global scope:
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:38:46: error: use of parameter outside function body before ‘]’ token
void copy_array(int m, int n, double source[m][n], double target[m][n])
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:38:49: error: use of parameter outside function body before ‘]’ token
void copy_array(int m, int n, double source[m][n], double target[m][n])
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:38:50: error: expected ‘)’ before ‘,’ token
void copy_array(int m, int n, double source[m][n], double target[m][n])
^
/home/drongh/ProgramProjects/c/c primer plus/answer10_8.c:38:52: error: expected unqualified-id before ‘double’
void copy_array(int m, int n, double source[m][n], double target[m][n])
上次由 drongh 在 2014-12-12 10:13,总共编辑 1 次。
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

Re: c语言变长数组拷贝问题?

#2

帖子 drongh » 2014-12-12 10:12

根本不知道哪里错,看看没问题。
谢谢。。
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

Re: c语言变长数组拷贝问题?

#3

帖子 oneleaf » 2014-12-12 10:39

数组作为参数的时候,数组的长度不能是变量。

可以考虑将类似:

代码: 全选

void fred (int m, int n, double a[m][n]) { ... }
修改为:

代码: 全选

void fred (int m, int n, void *va)
{
    double (*a)[n] = (double (*)[n]) va;
    ...
 }
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

Re: c语言变长数组拷贝问题?

#4

帖子 cao627 » 2014-12-12 12:29

不要变量来决定数组的长度,将决定数组长度的n 和m定义成宏,子函数的形参名n和m ,换成别的字母

#include "stdio.h"
#define m 3
#define n 5
...

void display_array(int x, int y, double arr[m][n])
...

void copy_array(int x, int y , double source[m][n], double target[m][n])
walker.ma
帖子: 26
注册时间: 2014-11-05 7:10
系统: LinuxMint 17

Re: c语言变长数组拷贝问题?

#5

帖子 walker.ma » 2014-12-12 14:03

void copy_array(int m, int n, double source[m][n], double target[m][n])
这不是一个正确的定义方式
Linux Mint 17 Mate Edition
人啊,总得活出点信仰来
walker.ma
帖子: 26
注册时间: 2014-11-05 7:10
系统: LinuxMint 17

Re: c语言变长数组拷贝问题?

#6

帖子 walker.ma » 2014-12-12 14:22

walker.ma 写了:void copy_array(int m, int n, double source[m][n], double target[m][n])
这不是一个正确的定义方式
我错了,这个定义是合理的,原因是souce[m][n],变量型长度的数组不能进行初始化赋值。
Linux Mint 17 Mate Edition
人啊,总得活出点信仰来
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

Re: c语言变长数组拷贝问题?

#7

帖子 drongh » 2014-12-12 16:16

代码: 全选

#include "stdio.h"
#define ROWS 3
#define COLS 4

int sum2d(int rows, int cols, int ar[rows][cols]);

int main(int argc, char const *argv[])
{
    int i, j;
    int rs = 3;
    int cs = 10;
    int junk[ROWS][COLS] = {
        {2, 4, 6, 8},
        {3, 5, 7, 9},
        {12, 10, 8, 6}
    };

    int morejunk[ROWS-1][COLS+2] = {
        {20, 30, 40, 50, 60, 70},
        {5, 6, 7, 8, 9, 10}
    };

    int varr[rs][cs];

    for (i = 0; i < rs; i++)
        for (j = 0; j < cs; j++)
            varr[i][j] = i * j + j;

    printf("3x5 array\n");
    printf("Sum of all elements = %d\n", 
        sum2d(ROWS, COLS, junk));

    printf("2x6 array\n");
    printf("Sum of all elements = %d\n",
        sum2d(ROWS-1, COLS+2, morejunk));

    printf("3x10 VLA\n");
    printf("Sum of all elements = %d\n", 
        sum2d(rs, cs, varr));

    return 0;
}

int sum2d(int rows, int cols, int ar[rows][cols])
{
    int r;
    int c;
    int tot = 0;
    for (r = 0; r < rows; r++)
        for (c = 0; c < cols; c++)
            tot += ar[r][c];
    return tot;    
}
上面是 C Primer Plus 第五版275页上的例子。云巅工作室译 人民邮电出版社。isbn号:978-7-115-13022-8/TP。
这个例子用了变长数值作为参量。
但是编译不过。是不是gcc编译选项没有打开???
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

Re: c语言变长数组拷贝问题?

#8

帖子 astolia » 2014-12-12 16:37

三楼和四楼可能不知道C99标准里的变长数组,6楼是对的,不能用那种方式初始化。
只需要把

代码: 全选

 double source[m][n] = {
      {1, 2, 3, 4, 5},
      {6, 7, 8, 9, 10},
      {11, 12, 13, 14, 15}
   };
改成

代码: 全选

double source[m][n];
   int row;
   int col;
   for (row = 0; row < m; row++)
   {
      for (col = 0; col < n; col++)
      {
         source[row][col] = row*5+col+1;
      }
   }
就成了,编译时记得加参数 gcc -std=c99 xxx.c。只是这是将变长二维数组复制给变长数组,和题目的要求可能不符。
上次由 astolia 在 2014-12-12 16:44,总共编辑 2 次。
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

Re: c语言变长数组拷贝问题?

#9

帖子 astolia » 2014-12-12 16:40

drongh 写了:

代码: 全选

#include "stdio.h"
#define ROWS 3
#define COLS 4

int sum2d(int rows, int cols, int ar[rows][cols]);

int main(int argc, char const *argv[])
{
    int i, j;
    int rs = 3;
    int cs = 10;
    int junk[ROWS][COLS] = {
        {2, 4, 6, 8},
        {3, 5, 7, 9},
        {12, 10, 8, 6}
    };

    int morejunk[ROWS-1][COLS+2] = {
        {20, 30, 40, 50, 60, 70},
        {5, 6, 7, 8, 9, 10}
    };

    int varr[rs][cs];

    for (i = 0; i < rs; i++)
        for (j = 0; j < cs; j++)
            varr[i][j] = i * j + j;

    printf("3x5 array\n");
    printf("Sum of all elements = %d\n", 
        sum2d(ROWS, COLS, junk));

    printf("2x6 array\n");
    printf("Sum of all elements = %d\n",
        sum2d(ROWS-1, COLS+2, morejunk));

    printf("3x10 VLA\n");
    printf("Sum of all elements = %d\n", 
        sum2d(rs, cs, varr));

    return 0;
}

int sum2d(int rows, int cols, int ar[rows][cols])
{
    int r;
    int c;
    int tot = 0;
    for (r = 0; r < rows; r++)
        for (c = 0; c < cols; c++)
            tot += ar[r][c];
    return tot;    
}
上面是 C Primer Plus 第五版275页上的例子。云巅工作室译 人民邮电出版社。isbn号:978-7-115-13022-8/TP。
这个例子用了变长数值作为参量。
但是编译不过。是不是gcc编译选项没有打开???
这个初始化时用的是宏定义ROWS和COLS,也就是说junk和morejunk都是定长数组,只有varr才是变长数组
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

Re: c语言变长数组拷贝问题?

#10

帖子 drongh » 2014-12-12 18:14

gcc c99的选项没打开。谢谢楼上各位。。

:em01
回复