Matrix pointer **x function

This is a discussion on Matrix pointer **x function within the C++ Programming forums, part of the General Programming Boards category; Hello, If i have this code Code: void init( char ** matrix) { matriz = new char*[5]; for(int i = ...

  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
    21,734
    Where do matriz and linhas come from?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 08:39 PM

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