1

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.

1
  • yea kinda... had to write a program using pointers, arrays and functions... was having trouble with the array declaration. Commented Feb 3, 2012 at 13:59

4 Answers 4

5

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot for the help... and my compiler was giving an error on the malloc so I had to use a=(int*)malloc(ROWS*COLS*sizeof(int));
@RaedShahid, then most probably you are compiling C code as C++. This isn't a good idea.
I guess so because I'm using Borland C++
1
   printf("Enter the number of rows: ");
   scanf("%d",&ROWS);
   printf("\nEnter the number of columns: ");
   scanf("%d",&COLS);

1 Comment

i always miss the ampersand :P
0

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)

2 Comments

it was still asking for a constant value for rows and cols.
Which compiler (and flags) do you use?
0
  1. You need to dynamically allocate the array - use malloc
  2. scanf("%d", &ROWS); - notice the & - scanf requires the address.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.