Problem with passing 2D array by reference

This is a discussion on Problem with passing 2D array by reference within the C Programming forums, part of the General Programming Boards category; Hi! I have a 2D array that I need to pass to a function, have its value modified within that ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    25

    Problem with passing 2D array by reference

    Hi! I have a 2D array that I need to pass to a function, have its value modified within that function, and then use the altered array within the original function.

    Here is a simplification of my code, along with a summary of my problem:

    Code:
    void function1(int arg1);
    void function2(double myArray[4][4]);
    
    void function1(int arg1){
    
       double myArray[4][4];
       int i = 0, j = 0;
    
      /* initialize array to 0 */ 
      for(i = 0; i < 4; i++){
         for(j = 0; j < 4; j++){
              myArray[4][4] = 0.0;
         }
       }
     
      function2(myArray); 
      /* print myArray values */ 
    
      /* problem: when I print out the resulting values of myArray here, they are wrong. How could they change from being correct within function2(), to being incorrect here? */ 
      
    
    }
    
    void function2(double myArray[4][4]){
    
      /* change values of myArray, and print myArray values; I've verified that *within* this function, the final myArray values *are* correct */ 
    
    }
    I've been referring to the following relevant C forum thread:
    Passing a 2d array by Reference

    I tried the double-pointer syntax suggested within that thread, as well, but I get compiler errors.

    Any suggestions about what I'm doing wrong, that would result in the myArray[][] values being correct when they're changed within function2(), but then are wrong when printed within function1()?

    Thanks in advance for your help!
    Last edited by CodeRed; 02-07-2012 at 09:26 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,339
    Perhaps it has to do with the way you're not initialising the array properly to begin with?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void function1(int arg1);
    void function2(double myArray[4][4]);
    
    void function1(int arg1){
    
       double myArray[4][4];
       int i = 0, j = 0;
    
      /* initialize array to 0 */
      for(i = 0; i < 4; i++){
         for(j = 0; j < 4; j++){
              myArray[i][j] = 0.0;  //!! NOT [4][4]
         }
       }
    
      function2(myArray);
      /* print myArray values */
    
      /* problem: when I print out the resulting values of myArray here, they are wrong. How could they change from being correct within function2(), to being incorrect here? */
      /* print again */
      for(i = 0; i < 4; i++){
         for(j = 0; j < 4; j++){
            printf("%f, ", myArray[i][j] );
         }
         printf("\n");
       }
    
    
    }
    
    void function2(double myArray[4][4]){
      /* change values of myArray, and print myArray values; I've verified that *within* this function, the final myArray values *are* correct */
       int i = 0, j = 0;
    
       /* assign */
      for(i = 0; i < 4; i++){
         for(j = 0; j < 4; j++){
              myArray[i][j] = i*j;
         }
       }
    
      /* print */
      for(i = 0; i < 4; i++){
         for(j = 0; j < 4; j++){
            printf("%f, ", myArray[i][j] );
         }
         printf("\n");
       }
    }
    int main ( ) {
      function1(3);
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    25
    Code:
     myArray[i][j] = 0.0;  //!! NOT [4][4]
    My apologies, that was a typo... my real code does use the [i][j] array subscripts. Apart from that, do you see any problems?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,339
    No, my example works just fine.

    Perhaps you should post some real code which demonstrates the problem, and not a facsimile which doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    25
    Perhaps you should post some real code which demonstrates the problem, and not a facsimile which doesn't.
    hmm, ok, I'll elaborate on my code, below. Thanks. (note: my code is a lot more extensive than what I've shown below, so it's not possible to include it all; but I have re-checked that I have captured the essence of what I'm doing, below. I know that the pseudocode below isn't much different from that in my OP, but literally, I call function2(), and between the end of function2() and its return within function1(), somehow the value of the TM[][] array changes...)

    Code:
    void function1(int arg1); void function2(double TM[4][4]); void function1(int arg1){ double TM1[4][4], TM2[4][4]; int i = 0, j = 0; /* initialize array to 0 */ for(i = 0; i < 4; i++){ for(j = 0; j < 4; j++){ TM1[i][j] = 0.0; TM2[i][j] = 0.0; } } function2(TM1); function2(TM2); /* print TM1 and TM2 values -- these *don't* match the respective TM1 and TM2 values printed within function2() */ } void function2(double TM[4][4]){ /* individually assign value to each element of TM[][], and print all TM[][] values here; I've verified that *within* this function, the final TM[][] values *are* correct */ }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,339
    And how exactly does that demonstrate the problem?

    If I can't copy / paste / compile / run and see the problem, I can't help you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    25
    Thanks for your input. It looks like I'm going to have to simplify my own code and see where it's going wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 01:25 AM
  2. Passing array by reference
    By TBc in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2005, 07:48 AM
  3. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 06:19 AM
  4. passing a reference to an array?
    By Captain Penguin in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2002, 03:10 PM
  5. Passing Array by Reference...
    By Cheeze-It in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2002, 12:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21