a
has a type int**
(pointer to a pointer). (a+i)
has the same type, *(a + i)
is a pointer, *(a + i) +j
is a pointer as well. The problem is you're trying to assign to a temporary object, which was created by the '+' operator. In fact, you're asking the compilator to solve an equation for you and then modify *(a + i)
, as it's the only value in your expression that is stored in memory.
The only thing you can do it assign either to *(a+i)
(and therefore set location of the row) or to *(*(a+i)+j)
(and set value of a particular element in the row), or to a
itself (and set location of pointers to rows).
This is not a Java and int doesn't have a NULL value. NULL in C is a macro which expands to zero, which can be casted to a null pointer afterwards.
So, allocation of (n+1) elements is useless if user types zero. You can, however, change a
's type to int***
and store in each cell either pointer to a single int
or NULL. However, this is a greate overhead and I wouldn't do this. You'd better store length of each row as its first element.
And, as Medinoc suggested, use []
instead of asterisks and pluses. a[b]
is the same as *(a+b)
.