-1

What is the difference between:

int *array;
array = (int*) calloc(5, sizeof(int));

and

int *array;
array = calloc(5, sizeof(int));

I don't get it. Both samples work. At the university the professor explained why you need the (int*) before the calloc, but I didn't get it.

In the course was a code sample like this:

struct data{
   int number;
   char *name;
};
typedef struct data student;

int main(){
    student **list;
    list = (student*) calloc(10, sizeof(student*));

    //Create structures dynamic in list
    ....

    return 0;
}

I hope someone can explain it to me. Thank you.

1 Answers1

0

The difference is that, first causes constraint violation while second is not. Return type of calloc is void *, therefore no need to cast it.

Also read: Do I cast the result of malloc?

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264