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 612701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:55:19+00:00 2026-05-13T17:55:19+00:00

Consider this code: #include <stdio.h> #define N 5 void printMatrix(int (*matrix)[N],int n) { int

  • 0

Consider this code:


#include <stdio.h>
#define N 5
void printMatrix(int (*matrix)[N],int n)
{
   int i,j;
   for(i=0;i<n;i++){
      for(j=0;j<n;j++)
        printf("%d",matrix[i][j]);
     printf("\n");
   }
}
int main()
{
   int R[N][N]={{1,2,3},{4,5,6},{7,8,9}};
   printMatrix(R,3);
}

This works fine as expected.
Now, I thought to write the functions handling 2D-matrices in a separate source file and link them wherever required.
But then I ran into a problem as in the function printMatrix, the size of array of int to which matrix points (i.e N) is required at compile-time. So, my functions would not work in other cases when the size is different.

So,How can I handle this?
Dynamic Arrays are a solution but i want to know if it can be done with static arrays.

  • 1 1 Answer
  • 1 View
  • 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-13T17:55:19+00:00Added an answer on May 13, 2026 at 5:55 pm

    You can’t use the built-in 2D array type if both sizes are not known at compile time. A built-in 2D array must have at least one of the two sizes known at compile time.

    If both sizes are run-time values, then you have no other choice but to use a “manual” implementation of 2D array, like an array of pointers to arrays, for example. In that case the function declaration might look as follows (two alternative equivalent forms)

    void printMatrix(int *const *matrix, int n, int m);
    void printMatrix(int *const matrix[], int n, int m);
    

    To access to the array elements you can still use the “traditional” syntax

    matrix[i][j]
    

    The array itself would be created as follows (a simple example)

    int row0[] = { 1, 2, 3 };
    int row1[] = { 4, 5, 6 };
    
    int *matrix[2];
    matrix[0] = row0;
    matrix[1] = row1;
    
    printMatrix(matrix, 2, 3);
    

    But if you already have a matrix implemented as a built-in 2d array

    int matrix[2][3] = { ... };
    

    then just to be able to pass it to the above function you can “convert” it into the above form by using an additional temporary “row pointer” array

    int *rows[2];
    rows[0] = matrix[0];
    rows[1] = matrix[1];
    
    printMatrix(rows, 2, 3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this C code: #include stdio.h int main(void) { int count = 5; unsigned
Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending
Consider this code: one.c: #include <stdio.h> int one() { printf(one!\n); return 1; } two.c:
Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following code: #include<stdio.h> #define k 0.7 #define p 0.5 int main() {
Consider the following code: #include <stdio.h> int main() { printf(%d, 300 * 300 /
Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider the following code: #include <stdio.h> #include <ctype.h> char* Mstrupr(char* szCad); int main() {
Consider the code : #include <stdio.h> class Base { public: virtual void gogo(int a){
consider this simple and pointless code. #include <iostream> struct A { template<int N> void

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.