Posted on 1998-03-06
I'm taking a C-programming course this term and my instructor did not explain the use and syntax of calling the calloc function very well. My question is, does a statement that calls calloc to generate an address for a pointer always have to have a cast to convert it to the type of pointer for which it is generating the address? I'm using Symantec C++ for my compiler and it doesn't seem to care about the cast, but some of my classmates are using other compilers, such as Borland C++ 4.5, Turbo C 1.0 or Visual C++, and these compilers seem to require the cast to convert the return of the calloc function from a pointer to void to a pointer to char.
For example, is the cast (char*) required in the calling statement in the function definition below where STRINGMAX is defined as a symbolic constant 255 and arraySize equals 10:
void initStrArray(char *A, int arraySize)
for (i=0; i<arraySize; i++)
fprintf(stderr, "Enter a string: ");
A[i] = (char*) calloc(strlen(inLine)+1, sizeof(char));