0

I came across this code while looking for jagged arrays in C. I am finding it difficult to understand the need for typecasting the calloc function since calloc() and malloc() return pointers.

int rowNum,colNum,i,j;
int** table;
scanf("%d",&rowNum);

Why are we using a pointer to a pointer and what does the line below return?

table = (int**)calloc(rowNum+1,sizeof(int*));
for(i=0;i<rowNum;i++)
{

printf("The size of %d row",i+1);
scanf("%d",&colNum);
table[i] = (int*) calloc(colNum+1,sizeof(int));

What's happening in the above line? Is the pointer pointing to the base element of the ith row?

for(j=1;j<=colNum;j++)
    {
    //reading the elements in the row
    scanf("%d",&table[i][j]);.
    }
    table[i][0] = colNum;

    printf("The size of row [%d]= %d",i+1,table[i][0]);
    }

What is table pointing to here?

Siddhartha rao
  • 487
  • 1
  • 4
  • 9
  • 3
    Please format your code in a readable way. No, `malloc` and `calloc` don't need casts, and should not even be casted, becaucse this can hide subtle errors. Otherwise your question is not really clear. Do you need an explanation what a pointer to pointer does? – Jens Gustedt Aug 13 '16 at 09:29
  • 1
    So the problem is with the typecasting the returned pointer from the `calloc` and `malloc` functions? Nothing else? Please try to be more explicit. Also a possible duplicate of [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (if that's your only problem). – Some programmer dude Aug 13 '16 at 09:31
  • 1) You don't need such casts 2) Why are you allocating space for one more row and columns?, arrays are base 0 in C, use `table = calloc(colNum, sizeof(int));` and change `for(j=1;j<=colNum;j++)` to `for(j=0;j – David Ranieri Aug 13 '16 at 09:31
  • On jagged arrays: https://en.wikipedia.org/wiki/Jagged_array – alk Aug 13 '16 at 09:45
  • Made the question clearer. @JensGustedt And the first element of each row is being used to store the number of elements in that row. – Siddhartha rao Aug 13 '16 at 09:45

1 Answers1

4

Table is pointing to the first element of dynamic array of pointers. And then we make each of those pointers point to an array of integers inside the for loop.

How this is different from a 2 Dim array is because in a matrix all rows have the same number of columns.

Our requirement is to obtain a jagged array. Take an array A of 5 columns and another array B of 4 columns if you combine both together in different rows you get a jagged array.

Now since we must assign like

table=calloc(rowNum+1,sizeof(int*));
//allocate an array of rowNum pointers and save to table
...
table[i] = calloc(colNum+1,sizeof(int));
//Yes it is pointing to the first element of the i-th row

In other words each table[i] represents each row which has variable number of columns(here as given by user).

I guess these links can help you out

Community
  • 1
  • 1
Ilaya Raja S
  • 415
  • 5
  • 18
  • 1
    Not the downvoter but question is tagged C (not C++). Please, read [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – David Ranieri Aug 13 '16 at 09:33
  • 1
    "*As for type casting it is because ....*", unfortunately not. In C `void`-pointers do not need be converted explicitly. This different from C++. – alk Aug 13 '16 at 09:36
  • @AlterMann i changed that, bit hasty answering sorry. – Ilaya Raja S Aug 13 '16 at 09:37
  • "*Table is pointing to a dynamic array of pointers.*" also not quiet right. `table` is pointing to a pointer to `int`, an `int*`. Which *might* be an arrays 1st element. A pointer to an array of 42 `int*`s for example would look like this `int * (*pa)[42]`. – alk Aug 13 '16 at 09:38