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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:54:16+00:00 2026-05-23T23:54:16+00:00

void displayMatrix(int **ptr) { printf(%d %d \n,**ptr,*(*ptr+1)); *ptr++; printf(%d %d \n,**ptr,*(*ptr+1)); } Is it

  • 0
void displayMatrix(int **ptr)
{
  printf("%d %d \n",**ptr,*(*ptr+1));
  *ptr++;
  printf("%d %d \n",**ptr,*(*ptr+1));
}

Is it a correct way to pass 2×2 array to function ?

displayMAtrix(array); 
  • 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-23T23:54:16+00:00Added an answer on May 23, 2026 at 11:54 pm

    If you have a 2D array with automatic storage duration (e.g. int matrix[2][2];), then no, this is not the correct way to pass a 2D array.

    int f ( int, char ** )
    {
      int matrix[2][2];
      displayMatrix(matrix);
    }
    

    To be standard compliant (there are other ways to make it work, but this is the standard, recommended way), you need to declare displayMatrix() as:

    void displayMatrix ( int matrix[][2] );
    

    You must declare the size of each dimension (possibly excluding the first). The reason behind this lies with the way 2D arrays are stored in memory. Wikipedia has a decent article on row-major order explaining the layout.

    Alternate storage type

    If you’re allocating large matrices (e.g. for storing images), you’ll usually end up with double pointers because you’ll be allocating the memory differently. In this case, you usually have a 1D array of pointers with each item storing the pointer to a 1D array representing rows (or columns).

    In that case, you would get something like:

    // this function is over-simplified.  it may leak memory if any
    // but the first `new` throws `std::bad_alloc`.
    int ** new_matrix ( int m, int n )
    {
      int ** matrix = new int*[m];
      for (int i = 1; (i < m); ++i ) {
        matrix[i] = new int[n];
      }
      return (matrix);
    }
    
    void displayMatrix ( int ** matrix, int m, int n );
    
    int main ( int, char ** )
    {
      int **const matrix = new_matrix(2, 2);
      displayMatrix(matrix, 2, 2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void turtle (int gtot) { int msg; fcntl(gtot,F_SETFL,O_NONBLOCK); read(gtot,&msg,4); gotoxy(12, 21); printf(The value of
void (int a[]) { a[5] = 3; // this is wrong? } Can I
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
void some_func(int param = get_default_param_value());
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
public void test() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); for (int
typedef void (FunctionSet::* Function)(); class MyFunctionSet : public FunctionSet { protected: void addFunctions() {
void pushSynonyms (string synline, char matrizSinonimos [1024][1024]){ stringstream synstream(synline); vector<int> synsAux; int num; while
void main() { const int * a; *a = 5; } gcc error :
void max_min(sqlite3 *db) { //call back********* int i, ncols; sqlite3_stmt *stmt; char *sql; const

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.