Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:14:43+00:00 2026-05-29T04:14:43+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:14:44+00:00Added an answer on May 29, 2026 at 4:14 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a program in which main calls other functions that test
How to write a java program which will tell me whether I have internet
i have question how write program which calculates following procedures http://en.wikipedia.org/wiki/Tetration i have exponential
suppose we have two numbers i want write program which print common bits subsequent
I have a python program which takes input from stdin. Now, I've to write
I have the string: foo$bar@baz I'm looking to write a C program which will
I have an interesting task: to write a program which captures input from the
I have been trying to write a C program which generates all possible permutations
i have to write program, which will create two child processes These processes would
I have been given a project where i need to write a program which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.