Thread: Matrix pointer **x function

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    21

    Matrix pointer **x function

    Hello,

    If i have this code

    Code:
     
    void init( char ** matrix)
    {
    	matriz = new char*[5];
    	for(int i = 0 ; i < linhas; i++)
    		matriz[i] = new char[5];
    
    	for(int i = 0; i < 5; i++)
    		for(int j = 0 ; j <5; j++)
    			matriz[i][j] = 'm';
    }
    int main()
    {
          char **matrix;
          init(matrix);
          cout << matrix[0][0];
    }
    this code return a access violation, so what can i do for the changes in function init on variable matrix can be aply on main function?
    sorry about my english

    []

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    21
    i forgot to change variable linhas, in his place must be a 5.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Assuming the matriz variable is actually a mistype of matrix, I would say the problem is:
    Code:
    	matriz = new char*[5];
    This line doesn't change your original value of matrix in main. You need to do one of two things:
    Option 1: Return the pointer to your matrix as a result
    Code:
    char **init(void)
    {
        char ** matrix;
    ...
        return matrix;
    }
    
    int main()
    {
       char **matrix = init();
       ...
    }
    Option 2: Use another indirection level.
    Code:
    void init( char *** matrix)
    {
    	*matrix = new char*[5];
    	for(int i = 0 ; i < 5; i++)
    		(*matrix)[i] = new char[5];
    
    	for(int i = 0; i < 5; i++)
    		for(int j = 0 ; j <5; j++)
    			(*matrix)[i][j] = 'm';
    }
    
    int main()
    {
          char **matrix;
          init(&matrix);
    ...
    }
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where do matriz and linhas come from?
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > matriz = new char*[5];
    But your parameter is matrix

    In order to change the variable in main, you need to pass a reference to it, like so.
    void init( char ** &matrix)
    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.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    21
    thanks for help.. Salem ideia works weel =)

    for laserlight the matriz is matrix, i make a mistake when i did some changes on code, and linhas i explain in top that in it place must be a 5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM