# creating 5X3 array using double astrix

• 03-30-2009
transgalactic2
creating 5X3 array using double astrix
*p is the 0 row
*p+1 is the first row
etc..
and i apply to each row a column

why its not working
??
Code:

```#include <stdio.h> #include <stdlib.h> int main() {         int i=0;     int **p=(int**)malloc(5*sizeof(int*));         for (i=0;i<5;i++)         {           *p+i=(int*)malloc(3*sizeof(int));         }         return 0; }```
• 03-30-2009
PING
Change that to
Code:

`p[i] = malloc ...`
Don't cast the return value of malloc.
• 03-30-2009
itCbitC
Looks like you forgot the parenthesis (in red).
Quote:

Originally Posted by transgalactic2
*p is the 0 row
*(p+1) is the first row
etc..
and i apply to each row a column

why its not working
??
Code:

```#include <stdio.h> #include <stdlib.h> int main() {         int i=0;     int **p=(int**)malloc(5*sizeof(int*));         for (i=0;i<5;i++)         {           *(p+i)=(int*)malloc(3*sizeof(int));         }         return 0; }```

• 03-30-2009
transgalactic2
Quote:

Originally Posted by itCbitC
Looks like you forgot the parenthesis (in red).

so this code could be interpretative as p[3][5] and as p[5][3]
??
• 03-30-2009
itCbitC
Quote:

Originally Posted by transgalactic2
so this code could be interpretative as p[3][5] and as p[5][3]
??

According to the storage allocations it can be interpreted only as p[5][3].
• 03-30-2009
transgalactic2
as i see it
we can say *(p+1) is the first column instead of
*(p+1) is the first row

how the storage allocation contradicts it?
• 03-30-2009
matsp
What you intepret as column or row is entirely up to you. However, the dimensions that you allocate is correct for:
Code:

`p[5][3];`
but not for
p[3][5];
[/code]
as you would walk outside the memory allocated here:
Code:

`malloc(3*sizeof(int))`
if you go beyond 2 in the second index.

--
Mats
• 03-30-2009
transgalactic2
*(*(p+2)+1) equals p[2][1]
?
• 03-30-2009
itCbitC
Quote:

Originally Posted by transgalactic2
*(*(p+2)+1) equals p[2][1]
?

Reading it from the inside out might help:

p: pointer to pointer to int ie &p[0]
p+2: points 2 elements past p ie &p[2]
*(p+2): dereferences to give p[2]
*(p+2)+1: points to the 2nd element of p[2] ie &p[2][1]
*(*(p+2)+1): dereferences to give p[2][1]
• 03-30-2009
transgalactic2
thanks:)
• 03-30-2009
BEN10
Quote:

Originally Posted by transgalactic2
*(*(p+2)+1) equals p[2][1]
?

for arrays
*(p+i)=p[i]=i[p]=*(i+p)
*(*(p+i)+j)=p[i][j]