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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:11:00+00:00 2026-06-12T12:11:00+00:00

I created two 2D arrays (matrix) in C in two different ways. I don’t

  • 0

I created two 2D arrays (matrix) in C in two different ways.
I don’t understand the difference between the way they’re represented in the memory, and the reason why I can’t refer to them in the same way:

scanf("%d", &intMatrix1[i][j]); //can't refer as  &intMatrix1[(i * lines)+j])

scanf("%d", &intMatrix2[(i * lines)+j]); //can't refer as &intMatrix2[i][j])

What is the difference between the ways these two arrays are implemented and why do I have to refer to them differently?

How do I refer to an element in each of the arrays in the same way (?????? in my printMatrix function)?

int main()
{
   int **intMatrix1;
   int *intMatrix2;

   int i, j, lines, columns;

   lines = 3;
   columns = 2;

   /************************* intMatrix1 ****************************/

   intMatrix1 = (int **)malloc(lines * sizeof(int *));

   for (i = 0; i < lines; ++i)
      intMatrix1[i] = (int *)malloc(columns * sizeof(int));

   for (i = 0; i < lines; ++i)
   {
       for (j = 0; j < columns; ++j)
       {
       printf("Type a number for intMatrix1[%d][%d]\t", i, j);
       scanf("%d", &intMatrix1[i][j]); 
       }
   }

   /************************* intMatrix2 ****************************/ 

   intMatrix2 = (int *)malloc(lines * columns * sizeof(int));

   for (i = 0; i < lines; ++i)
   {
       for (j = 0; j < columns; ++j)
       {
       printf("Type a number for intMatrix2[%d][%d]\t", i, j);
       scanf("%d", &intMatrix2[(i * lines)+j]);
       }
   }

   /************** printing intMatrix1 & intMatrix2 ****************/

   printf("intMatrix1:\n\n");
   printMatrix(*intMatrix1, lines, columns);

   printf("intMatrix2:\n\n");
   printMatrix(intMatrix2, lines, columns);
}


/************************* printMatrix ****************************/

void printMatrix(int *ptArray, int h, int w)
{
    int i, j;

    printf("Printing matrix...\n\n\n");

    for (i = 0; i < h; ++i)
        for (j = 0; j < w; ++j)
        printf("array[%d][%d] ==============> %d\n, i, j, ??????);
}
  • 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-06-12T12:11:01+00:00Added an answer on June 12, 2026 at 12:11 pm

    You are dereferencing the Matrix1 two times..

    Matrix1[i][j] ;
    

    It means that it is a 2D array or a double pointer declared like this.

    int **Matrix1 ;
    

    A double pointer could be thought of as array of pointers. Its each element is a pointer itself, so it is dereferenced once to reach at the pointer element, and dereferenced twice to access the data member of that member pointer or array. This statement as you you wrote is equivalent to this one..

    Matrix1[i][j] ;   //is ~ to
    
    *( *(Matrix1 + i) + j) ;
    

    For a single pointer like this.

    int *Matrix2 ;
    

    You can derefernce it only once, like this.

    Matrix2[i] ;  //is ~ to
    *(Matrix2 + i) ;
    

    This statement which you wrote..

    Matrix2[(i * lines)+j] ;
             |-----------|
    

    This portion evaluates to a single number, so it derefenced one time.

    (i * lines) + j ;
    

    As for your printmatrix() function, the ptArray passed to it is a single pointer. So you cannot dereference it twice.

    Perhaps you can get better understanding of static and dynamic 2D arrays from my answer here.

    2D-array as argument to function

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

Sidebar

Related Questions

I created two arrays. First the arrays show the elements with no order whatsoever.
Is there a way to create two dimensional NSArray without nesting arrays in the
I have created two arrays. I wanna pass these two array to any function.
I've got two arrays of Tasks - created and assigned. I want to remove
i have two arrays (actually one, but i created two for each columns). I
I have two integer arrays created at runtime (size depends on the program input).
I have two cell arrays that are <12x1 cell> and they are labeled A
I've created a hashtable of two-dimensional arrays in c# and cannot figure out how
I have two arrays that I create like this: public int GameBoard[][] = new
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a

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.