# 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. ## 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. i forgot to change variable linhas, in his place must be a 5.

3. 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);
...
}```

4. Where do matriz and linhas come from?

5. > 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)

6. 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