【问题标题】:How to initialize array elements before using it?如何在使用之前初始化数组元素?
【发布时间】:2012-02-03 11:28:07
【问题描述】:

我必须编写一个程序来说明指针与数组和函数的用法。

#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");
   }
}

这个程序给出了一个错误,即在声明变量 ROWS 和 COLS 之前正在使用它们。如何解决这个问题。

【问题讨论】:

  • 是的,有点...不得不使用指针、数组和函数编写程序...在数组声明方面遇到了问题。

标签: c arrays initialization


【解决方案1】:

一种选择是在堆上分配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();
}

【讨论】:

  • 非常感谢您的帮助...我的编译器在 malloc 上出现错误,所以我不得不使用 a=(int*)malloc(ROWS*COLS*sizeof(int));
  • @RaedShahid,那么您很可能正在将 C 代码编译为 C++。这不是一个好主意。
  • 我猜是因为我使用的是 Borland C++
【解决方案2】:
   printf("Enter the number of rows: ");
   scanf("%d",&ROWS);
   printf("\nEnter the number of columns: ");
   scanf("%d",&COLS);

【讨论】:

  • 我总是想念与号:P
【解决方案3】:

你应该在你得到rowscols之后声明这个数组——否则没有意义。

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

顺便说一句,main 应该返回int(如果程序成功结束则为0)

【讨论】:

  • 它仍然要求为行和列提供一个常量值。
  • 您使用哪种编译器(和标志)?
【解决方案4】:
  1. 您需要动态分配数组 - 使用malloc
  2. scanf("%d", &amp;ROWS); - 注意 &amp; - scanf 需要地址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多