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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:34:33+00:00 2026-06-17T13:34:33+00:00

I am trying to pass a matrix to a function by reference. The function

  • 0

I am trying to pass a matrix to a function by reference. The function will replace every element A[i][j] of the matrix by -A[i][j]. I first create the matrix:

float a[3][4] =
{
    {1.0f, 0.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f, 0.0f},
    {1.0f, 1.0f, 0.0f, 0.0f},
};

Then, I obtain the pointer to this matrix:

float*** pa = &a;

Then, I introduce the following function:

void process(float ***matrix, int nRows, int nCols){
 short i;
 short j;
 for (i=0 ; i<nRows; i++){
   for (j=0 ; j<nCols ; j++){
     (*matrix)[i][j] *= -1;
   }
 }
}

which I call as follows:

process(pa,3,4);

My program fails to execute and returns:

Segmentation fault: 11

Any ideas?

Summary of the answers: Some notes based on the questions this question received:

I. The aforementioned function can be used, provided that a is initialized a bit differently so as to be a float**. In particular:

int numberOfRows = 3;
int numberOfColumns = 4;
float **a = (float **) malloc(sizeof (float *) * numberOfRows);
for (i = 0; i < numberOfRows; ++i) {
    a[i] = (float *) malloc(sizeof (float) * numberOfColumns);
}

and then, it is passed to the function process as process(&a, 3,4);.

II. Alternatively, one may use the function:

void multi_by_minus(float *matrix, int nRows, int nCols) {
  short i,j;
  for (i = 0; i < nRows; i++) {
      for (j = 0; j < nCols; j++) {
          matrix[i * nCols + j] *= -1;
      }
  }
}

which treats the matrix as an one-dimensional array. In that case we simply invoke it as multi_by_minus(&a, 3, 4);

III. Finally, we may use the method:

void process2(int nRows, int nCols, float (*matrix)[nCols]) {
  short i, j;
  for (i = 0; i < nRows; i++) {
    for (j = 0; j < nCols; j++) {
        matrix[i][j] *= -1;
    }
  }
}

to which we provide a pointer to a, i.e., we invoke it like process2(3,4,&a);. In this way, we acquire access to the elements of the matrix in 2D.

  • 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-17T13:34:35+00:00Added an answer on June 17, 2026 at 1:34 pm

    There is no need for the triple pointer since you are already supplying the memory. You would use that if you were to allocate the memory inside de function.

    You can’t index a 2 dimension matrix without supplying at least the size of 1 dimension. The reason is that the compiler needs to generate code to access the correct offset taking into account both dimensions. In this particular case, I suggest passing a simple pointer and indexing as a 1D array, like this:

    void process(float *matrix, int nRows, int nCols){
     short i;
     short j;
     for (i=0 ; i<nRows; i++){
       for (j=0 ; j<nCols ; j++){
         matrix[i * nCols + j] *= -1;
       }
     }
    }
    

    You can then call it like this:

    process((float*)a,3,4);
    

    This way you manually index your buffer.

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

Sidebar

Related Questions

Trying to pass a string argument to a function, which will then be used
I am trying to pass a matrix to dct2 function but it is showing
I am trying to create a pointer to the start of a matrix to
im trying to pass two parameters to a function, i being an int value
In my project I've these files: functions.h functions.cc main.cc I'm trying to pass Matrix
Im trying to pass two JavaScript variables to PHP variables. function myFunction(){ var one
I'm trying to pass a user-defined array (defined here as matrix1) into a function
I am trying to create a matrix with dynamic proportions and to initialize it
trying to pass a function name as a variable in order to call it
I'm trying to pass a numpy matrix to an object method but I keep

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.