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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:26:16+00:00 2026-06-01T03:26:16+00:00

I want to transpose a matrix in C, with as little memory use as

  • 0

I want to transpose a matrix in C, with as little memory use as possible. Therefore it is not an option to simply create a copy of the matrix, fill it in, and delete the other one afterwards. I thus want to do every swap on the matrix itself, with 1 temp variable to reuse. Matrix elements are indexed with pointer arithmetic, but as far as I’m aware, these are correct. The problem now being that the function doesn’t result in any changes. The matrix struct is dynamically allocated and has the folowing structure;

typedef struct {
int rows;
int cols;
int** data;
    } matrix;

The transpose function I wrote now looks as follows;

void transpose(matrix* m){
int i,j,temp;
for(i=0;i<m->row;i++){
    for(j=0;j<m->col;j++){
        temp=*((*(m->data+i))+j);
        *((*(m->data+i))+j)=*((*(m->data+j))+i);
        *((*(m->data+j))+i)=temp;
    }
}

}

I already tried starting the function with

int* temp=(int*)malloc()sizeof(int);

and ending with

 free(temp);

That gives me an error in Visual Studio about adres acess. Any tips?

  • 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-01T03:26:17+00:00Added an answer on June 1, 2026 at 3:26 am

    You are indexing over the entire matrix in your for-loop (and I’m assuming your matrix is square) … you only need to index over the lower-triangle or upper triangle of the matrix, and then swap those positions with the corresponding index in the opposing triangle of the matrix. To transpose the diagonal of the matrix, you only need to swap the upper-half of the diagonal with the lower half. By indexing across the entire matrix you end up doing a double-transpose which will end up returning the exact same result as the original matrix.

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

Sidebar

Related Questions

I want to transpose a matrix, its a very easy task but its not
Hey! I have matrix class and i want to add, multiply and take transpose
I want to create a transpose function for dynamic 2d arrays. I want the
So the obvious way to transpose a matrix is to use : for( int
I want to split and transpose a multiline string from one DataGridView(dgvObs) to separate
I want to write an app to transpose the key a wav file plays
I'm very new to Mathematica. I want to use it as a data source
I have an 8x16 matrix of bits as a UINT8 matrix[16]. I want to
I have a table and want to transpose its rows to columns, similar to
I want to run a function on all periods of an xts matrix. apply()

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.