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

  • Home
  • SEARCH
  • 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 1061013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:24:51+00:00 2026-05-16T18:24:51+00:00

I need to print out a 3 by 3 matrix in C. I could

  • 0

I need to print out a 3 by 3 matrix in C. I could think up of 3 different ways of doing it. Are all of them equally optimal or is one method better than the other?

Function 1: Passing an 2darray, using array subscripts

void printMatrix(int m[][3])
{
 int i,j;

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d\t",m[i][j]);
  }
  printf("\n");
 }
}

Function 2: Passing a dbl pointer, using ptr arithmetic

void printMatrix(int **m)
{
 int i,j;

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d\t", *(*(m+i)+j) );
  }
  printf("\n");
 }
}

Function 3: Passing a dbl pointer, using ptr arithmetic (again)

void printMatrix(int **m)
{
 int i,j;

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d\t",**m);
   (*m)++;
  }
  (*m)=(*m)-3;
  m++;
  printf("\n");
 }
}
  • 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-16T18:24:52+00:00Added an answer on May 16, 2026 at 6:24 pm

    Function 1 operates on a different data type than functions 2 and 3.

    For function 1, the data has to be defined as int foo[3][3] or allocated as a block of 9 ints. With C89, the second 3 must be a compile-time constant, which could be problematic. This could be worked around by passing a simple int * and indexing using matrix[i * dim2 + j]. If you have C99, you can use variable length arrays and variably modified types to keep the nice matrix[i][j] notation, but the declarations may be a bit more complicated and there are some restrictions.

    Functions 2 and 3 work on several pieces of data: an array of integer pointers which point to additional arrays of integers. The extra indirection may allow some ease of use and flexibility (for example, a triangular matrix) but may reduce performance.

    The choice between function 2 and function 3 is pretty clear to me: use function 2. Changing a data structure for what should be a read-only operation should be avoided if possible, even if you put it back the way it was: it prevents concurrent use and you might do the “put it back” part incorrectly, leading to hard to diagnose problems. The expression *(*(m+i)+j) can be changed to m[i][j] even though it does something different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a homework assignment in which I need to print out to the
I need to print out only one of various consecutive lines with same first
I need to print out data into a pre-printed A6 form (1/4 the size
i need to print out numbers 1-100 in a random order. the print statement
I need to print a receipt from my web based apps using dot matrix
I need to print out an easy to read object graph from Visual Studio
I'm currently working on a project where I need to print out a lesson
For my app, I need to print out a series of outputs and then
When I am given d\/b , I need to print out the statement character
I have a report I need to print out in an application I'm usually

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.