Raed Shahid
Raed Shahid

Reputation: 241

How to initialize array elements before using it?

I have to write a program which illustrates the usage of pointers with arrays and functions.

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

The above program prints a matrix with the rows and columns predefined. I want to modify the program such that the rows and columns are entered by the user.

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

This programs is giving an error that the variables ROWS and COLS are being used before they are being declared. How to solve this problem.

Upvotes: 1

Views: 431

Answers (4)

NPE
NPE

Reputation: 500883

One option is to allocate a on the heap:

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

Notice also that I've:

  1. Added ampersands that were missing from the scanf() calls;
  2. Changed the return type of main() to int. See What are the valid signatures for C's main() function?

As you why your code didn't work:

Traditionally, C required constant expressions for array bounds. When ROWS and COLS were constants, everything was good with your code. Once you've turned them into variables, a became a variable-length array. The problem was that the size of the array is computed at the point where the array is declared, and at that point the values of ROWS and COLS were not yet known.

In C99, it is possible to fix your code by pushing the declaration of a down:

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();
}

Upvotes: 5

George
George

Reputation: 116

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

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60037

  1. You need to dynamically allocate the array - use malloc
  2. scanf("%d", &ROWS); - notice the & - scanf requires the address.

Upvotes: 0

asaelr
asaelr

Reputation: 5456

You should declare the array after you get rows and cols - otherwise it's not make sense.

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

By the way, main should return int (0 if the program ends successfully)

Upvotes: 0

Related Questions