2012-02-03 63 views
1

我必须写一个程序,它示出了具有阵列和函数指针的使用。如何在使用前初始化数组元素?

#include <stdio.h> 
#include <conio.h> 

#define ROWS 3 
#define COLS 4 

void print(int rows, int cols, int *matrix); 

void main(void) 
{ 
    int a[ROWS*COLS],i; 
    for(i=0;i<ROWS*COLS;i++) 
    { 
     a[i]=i+1; 
    } 
    print(ROWS,COLS,a); 
    getch(); 
} 

void print(int rows, int cols, int *matrix) 
{ 
    int i,j,*p=matrix; 
    for(i=0;i<rows;i++) 
    { 
    for(j=0;j<cols;j++) 
     { 
     printf("%3d",*(p+(i*cols)+j)); 
     } 
     printf("\n"); 
    } 
} 

上述程序打印预定义行和列的矩阵。我想修改程序,以便用户输入行和列。

#include <stdio.h> 
#include <conio.h> 

void print(int rows, int cols, int *matrix); 

void main(void) 
{ 
    int ROWS,COLS,a[ROWS*COLS],i; 
    printf("Enter the number of rows: "); 
    scanf("%d",ROWS); 
    printf("\nEnter the number of columns: "); 
    scanf("%d",COLS); 
    for(i=0;i<ROWS*COLS;i++) 
    { 
     a[i]=i+1; 
    } 
    print(ROWS,COLS,a); 
    getch(); 
} 

void print(int rows, int cols, int *matrix) 
{ 
    int i,j,*p=matrix; 
    for(i=0;i<rows;i++) 
    { 
    for(j=0;j<cols;j++) 
     { 
     printf("%3d",*(p+(i*cols)+j)); 
     } 
     printf("\n"); 
    } 
} 

这个节目是给一个错误,被宣布之前,他们正在使用的变量行列数。如何解决这个问题呢。

+0

这功课吗? – 2012-02-03 11:32:14

+0

是呀...不得不写一个使用指针,数组和函数的程序......在数组声明中遇到了麻烦。 – 2012-02-03 13:59:20

回答

5

一种选择是在堆上分配a

int main(void) 
{ 
    int rows,cols,*a,i; 
    printf("Enter the number of rows: "); 
    scanf("%d",&rows); 
    printf("\nEnter the number of columns: "); 
    scanf("%d",&cols); 
    a = malloc(rows*cols*sizeof(int)); 
    for(i=0;i<rows*cols;i++) 
    { 
     a[i]=i+1; 
    } 
    print(rows,cols,a); 
    getch(); 
    free(a); 
} 

还请注意,我有:

  1. 添加&符号从scanf()电话中缺少的;
  2. 改变的main()返回类型int。见What are the valid signatures for C's main() function?

正如你为什么你的代码没有工作:

传统上,C要求数组边界常量表达式。当ROWSCOLS是常量时,你的代码一切都很好。一旦他们变成变量,a成了variable-length array。问题是,阵列的大小被计算在其中阵列被声明的点,并在该点的ROWSCOLS的值尚未公知的。

在C99,可以通过推a下来的声明来修复代码:

int main(void) 
{ 
    int rows,cols,i; 
    printf("Enter the number of rows: "); 
    scanf("%d",&rows); 
    printf("\nEnter the number of columns: "); 
    scanf("%d",&cols); 
    int a[rows*cols]; 
    for(i=0;i<rows*cols;i++) 
    { 
     a[i]=i+1; 
    } 
    print(rows,cols,a); 
    getch(); 
} 
+0

非常感谢您的帮助...我的编译器是给在malloc的错误,所以我不得不使用'A =(INT *)malloc的(行* COLS *的sizeof(int)的);' – 2012-02-03 14:17:34

+0

@RaedShahid,然后很可能你正在将C代码编译为C++。这不是一个好主意。 – 2012-02-03 14:22:42

+0

我想是因为我使用Borland C++ – 2012-02-05 00:59:55

0

rowscols后,您应该声明数组 - 否则就没有意义了。

int rows,cols; 
scanf("%d %d",&rows,&cols); 
int a[rows*cols]; 

顺便说一句,main应该返回int(0,如果该计划成功结束)

+0

它没有为我工作,只好用malloc。 – 2012-02-03 14:00:44

+0

你会得到哪个错误? – asaelr 2012-02-03 14:25:03

+0

它仍然要求行和列的常量值。 – 2012-02-05 01:01:21

0
  1. 你需要动态分配数组 - 使用malloc
  2. scanf("%d", &ROWS); - 注意& - scanf要求地址。
+0

感谢您的帮助:) – 2012-02-03 14:01:01

1
printf("Enter the number of rows: "); 
    scanf("%d",&ROWS); 
    printf("\nEnter the number of columns: "); 
    scanf("%d",&COLS); 
+0

我总是错过&符:P – 2012-02-03 14:01:43

+0

它现在的作品,是吗? – George 2012-02-04 08:55:45

+0

是的,它现在工作...也需要malloc。 – 2012-02-05 00:58:07