# alocation of a matrix

• 01-23-2006
spank
alocation of a matrix
I have a matrix alocated dinamicaly:
Code:

```int **alocmat(int n) {         int **a,i=0;         a=(int**)calloc(n,sizeof(int*));         for(;i<n;i++) *(a+i)=calloc(n,sizeof(int));         return a; }```
This code is OK!

and now i try to read using pointers. the bolded line can work using a[i][j], but i want to write it using pointers. how can i do that?
Code:

```void citire(int** a,int n) {         int i=0,j;         for(;i<n;i++)                 for(j=0;j<n;j++)                 {                         printf("[%d][%d]: ",i,j);                         scanf("%d",*(*(a+i)+j));                 } }```
Thank you!
• 01-23-2006
itsme86
The problem is that scanf() expects the address of the location in which to store the data, but you're passing the data itself. Try just simply removing the first * so it would look like:
Code:

`scanf("%d", *(a+i)+j);`
Or alternatively you could do this I guess, if it makes it more clear to you what's going on:
Code:

`scanf("%d", &(*(*(a+i)+j)));`
Just like you would do:
Code:

```int a; scanf("%d", &a);  // RIGHT!```
```int a; scanf("%d", a);  // WRONG!```
```void tipar(int** a,int n) {         int i=0,j;         for(;i<n;i++)         {                 for(j=0;j<n;j++) printf("%d ",*(*(a+i)+j));                 printf("\n");         } }```