db2791
db2791

Reputation: 1110

Calloc a Two-Dimensional Array

To make a two dimensional array, I'm currently using the following:

int * own;

own = (int *)calloc(mem_size, sizeof(int));

for (i=0;i<mem_size;i++){
    own[i] = (int *)calloc(3, sizeof(int));
}

However, every time I reference own[i][j], I get an error saying that the subscripted value is neither array nor pointer nor vector.

Upvotes: 1

Views: 26773

Answers (5)

ali naderi
ali naderi

Reputation: 19

int *own = calloc(100, sizeof(int));

To build a array 2D array in memory is like this => enter image description here

So it can be accessed using such a formula

*(own + rows*y +x)

Using this type of array is very simple and open, for example when we have 100 cells from memory and we can make it into an array with a size of 100 divisible.
For example, in 100 homes, a array of this size can be provided =>
[1][100]= (own + 1y +x)
[2][50]= (own + 2y +x)
[4][25]= (own + 4y +x)
[5][20]= (own + 5y +x)
[10][10] = (own + 10y +x)
Sorry if I was not right because I speak Persian and I do not know much English


Example:

int *aPtr = calloc(100, sizeof(int));
for(int y=0;y<4;y++){
    for(int x=0;x<25;x++){
        *(own + 25*y +x) = 1;
    }
}

Upvotes: 0

chux
chux

Reputation: 154255

Wrong type for 2-D array - well pointed out by many.

Different suggested solution.

When allocating, use the sizeof the variable and not sizeof the type. Less likely to get it wrong - easier to maintain.

//int * own;
int **own;

own = calloc(row_count, sizeof *own);
for (i=0; i<row_count; i++){
  own[i] = calloc(column_count, sizeof *(own[i]));
}

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 181008

However, every time I reference own[i][j], I get an error saying that the subscripted value is neither array nor pointer nor vector.

That's right. own[i] is equivalent to *(own + i), which has type int. You cannot apply the subscript operator to an int.

Note that what your code appears to be trying to create is not a two-dimensional array, but rather an array of pointers. If that's really what you want then own should have type int **, and you should adjust the first calloc() call accordingly. If you really want dynamic allocation of a 2D array, though, then that would be:

int (*own)[3];

own = calloc(mem_size, sizeof(*own));

Note that there is no need then to allocate (or free) the rows separately.

Note also, however, that if you do not ever need to reallocate own before it goes out of scope, if you can assume at least a C99 compiler, and if mem_size is will never be too large then you can do this even more easily via a variable-length array:

int own[mem_size][3] = {{0}};

No explicit dynamic allocation or deallocation is needed at all in that case, and the initializer can be omitted if unneeded. (I include the initializer because calloc() performs equivalent initialization of the allocated space.) "Too large" should be interpreted in relation to the array being allocated on the stack.

Upvotes: 8

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

Because own is declared as having type int *

int * own;

then own[i] is a scalar object of type int and you may not apply to it the subscript operator.

You could write the following way

int ( *own )[3] = calloc( mem_size, 3 * sizeof( int ) );

The other way is the following

int **own = malloc( mem_size * sizeof( int * ) );

for ( i = 0; i < mem_size; i++ ) own[i] = calloc( 3, sizeof( int ) );

Upvotes: 1

R Sahu
R Sahu

Reputation: 206697

Use:

int ** own; // int**, not int*
own = calloc(mem_size, sizeof(int*)); //int*, not int
                                      // And remove the explicit cast.

Upvotes: 6

Related Questions