Thread: passing array 2D

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    passing array 2D

    Hi guys, I'm totally in shock why it's not working although I'm really good on pointers !
    I want to pass array 2D (MATRIX) to the called function and change on the same matrix some modifications on it, and I want to keep the changes on the matrixs itself even though I passing it and changing it the called function .

    my code:
    Code:
    void change(int **mat)
    {
    for (int i=0;i<2,i++)
       for(int j=0;j<2;j++)
              mat[i][j]=1;
    }
    
    int main()
    {
    int matrix={ 
          {0 0},
          {0 0} };
    change(matrix);
    for (int i=0;i<2,i++)
       for(int j=0;j<2;j++)
              printf("%d", matrix[i][j]);
    return 0;
    }
    I've ran this code, the compiler not showing me any errors but it's not printing the matrix after manipulation, it's printing the values of the matrix as what it was before doing function "change" , in other words the manipulation on the matrix didn't save. any clue? what's wondering my after doing the function change in the main function, the values of "matrix" after returning from the change function doesn't change at all .. like the function change didn't do anything ! weird!!
    I used pointer **mat to keep the change on the passed matrix however didn't save !

    any help please how can I solve the problem? thanks for your cooperation.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm surprised both your outer for loops compiled: I would have thought the typo error would result in a compile error.

    I'm surprised your compiler didn't warn you about the initialisation or the function call: perhaps you ignored the warning or didn't compile at a high enough warning level?

    If matrix wasn't an int, it would have been an int[2][2], which when passed as an argument is converted to an int(*)[2].

    Saying that you're really good at pointers doesn't mean it's true. You did inform me once that you are very good at C *shrugs*
    Last edited by laserlight; 05-03-2019 at 12:18 PM.
    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

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    I'm surprised both your outer for loops compiled: I would have thought the typo error would result in a compile error.

    I'm surprised your compiler didn't warn you about the initialisation or the function call: perhaps you ignored the warning or didn't compile at a high enough warning level?

    If matrix wasn't an int, it would have been an int[2][2], which when passed as an argument is converted to an int(*)[2].

    Saying that you're really good at pointers doesn't mean it's true. You did inform me once that you are very good at C *shrugs*
    but the syntax
    Code:
     int(*)[2]
    didn't work onto my compiler, it says "parameter omitted" .. I'm using clion compiler .. any help how can I pass matrix as an argument?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by RyanC View Post
    but the syntax
    Code:
     int(*)[2]
    didn't work onto my compiler, it says "parameter omitted" .. I'm using clion compiler .. any help how can I pass matrix as an argument?
    The clion, Google found, seems to be a IDE and not a compiler.
    Switching Compilers - Help | CLion
    In CLion, you can use GCC and Clang compilers.
    Tim S.
    "...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

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    didn't work onto my compiler, it says "parameter omitted" .. I'm using clion compiler .. any help how can I pass matrix as an argument?
    That's because you omitted the parameter name, as the compiler kindly informed you. Since this is a parameter, there's syntactic sugar: int mat[][2]

    You need to fix the other problems though.
    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
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    That's because you omitted the parameter name, as the compiler kindly informed you. Since this is a parameter, there's syntactic sugar: int mat[][2]

    You need to fix the other problems though.
    Thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing value into an array
    By Darius Dempsey in forum C++ Programming
    Replies: 9
    Last Post: 04-17-2014, 06:22 PM
  2. passing to array
    By TurdFergison in forum C Programming
    Replies: 2
    Last Post: 12-28-2010, 09:50 PM
  3. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  4. Passing array right
    By devilsknight in forum C Programming
    Replies: 7
    Last Post: 06-18-2006, 12:21 AM
  5. passing an array to a function (2d array)
    By revelation437 in forum C Programming
    Replies: 5
    Last Post: 03-11-2003, 03:32 PM

Tags for this Thread