Thread: Can't change global 2d array variable

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    4

    Can't change global 2d array variable

    I've tried creating a reference to the pointer and modifying that, like this, but that hasn't worked either.
    Code:
    float **temp = &inverseMatrix;
    temp[0][0] = 8;
    I've also tried setting the reference equal to another matrix by doing
    Code:
    float **temp = someMatrix;
    I have no clue where to go next. I've been reading forum posts and have tried adding a * in front but that just results in compilation errors. What is the correct way to modify a global variable from a method that is not main?
    Code:
    float** inverseMatrix;void change();
    
    
    void printMatrix(const float **matrix, int rows, int columns);
    int main()
    {
    	int j;
    	inverseMatrix = (float**)malloc(8 * sizeof(float *));
    	for (j = 0; j < 8; j++)
    	{
    		inverseMatrix[j] = (float*)malloc(8 * sizeof(float));
    
    
    	}
    	change();
    	printMatrix(inverseMatrix, 8, 8);
    }
    void change()
    {
    	for (int i = 0; i < 8; i++)
    	{
    		for (int j = 0; j < 8; j++)
    		{
    			inverseMatrix[i][j];
    		}
    	}
    	printMatrix(inverseMatrix, 8, 8);
    }

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    You didn't say exactly what's wrong, but line 24 doesn't do anything. You should be getting a warning for that line. Don't ignore warnings.

    What is it that you want line 24 to do?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,688
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    4
    Whoops. As I was trying to isolate the problem and reproduce it, I forgot to set it to a number. I meant to write [CODE]inverseMatrix[i][j] = j;[\CODE]. However, this still doesn't modify the matrix.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    Quote Originally Posted by ColeBacha View Post
    Whoops. As I was trying to isolate the problem and reproduce it, I forgot to set it to a number. I meant to write inverseMatrix[i][j] = j;. However, this still doesn't modify the matrix.
    Yes it does. What does your print function look like? It should look something like this.

    Code:
    void printMatrix(const int **m, int rows, int cols) {
      for(int r = 0; r < rows; r++) {
        for(int c = 0; c < cols; c++) {
          printf("%2d ", m[r][c]);
        }
        printf("\n");
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't change global variable :(
    By awsdert in forum Linux Programming
    Replies: 2
    Last Post: 02-04-2019, 09:22 AM
  2. User input variable to global array help
    By Bob_the_Builder in forum C Programming
    Replies: 5
    Last Post: 10-11-2013, 12:51 PM
  3. How do you change the size of a global array?
    By kbfirebreather in forum C Programming
    Replies: 10
    Last Post: 10-17-2009, 08:39 PM
  4. Replies: 2
    Last Post: 02-09-2006, 06:56 AM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM

Tags for this Thread