# Matrix pointer **x function

• 01-02-2008
skiler
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?

[]
• 01-02-2008
skiler
i forgot to change variable linhas, in his place must be a 5.
• 01-02-2008
matsp
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); ... }```
• 01-02-2008
laserlight
Where do matriz and linhas come from?
• 01-02-2008
Salem
> matriz = new char*[5];