• C

Casting calloc

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)
   char inLine[STRINGMAX];
   int i;

   for (i=0; i<arraySize; i++)
      fprintf(stderr, "Enter a string: ");
      A[i] = (char*) calloc(strlen(inLine)+1, sizeof(char));
      strcpy(A[i], inLine);
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zirconAuthor Commented:
Edited text of question
I believe that this is truely compiler dependent. However, I think ( please someone tell me if I am wrong) that you do need the cast to be standard.
No, it is not required. The point of the "pointer to void" concept is to allow any type of pointer to be passed around through the system without altering it.

However, it is now considered standard good coding practice to cast it when you assign such a pointer to a variable. This would catch potential subtle bugs later on, if the type of the variable that is being assigned the pointer were to change.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
To be more precise I should say, it is not required by the ANSI standard of the language. The "void *" was introduced by the standards body to formalize certain aspects of the language. However, one of the guiding principles of the standardization was: "don't break existing reasonable code" (a paraphrase I'm sure). Therefor constructs such as "cp=calloc(5,1);" which abound in existing code must continue to be compilable.

On the other hand, there are lots of subtle bugs that can be caused by these practices. This is the reason that the ANSI body introduced the opportunity for using various safeguards such as prototypes etc.

Therefore, when writing new code, it is good professional practice to cast such pointers.

I assume that the compilers that are "requiring" the cast can be "reduced" to accepting standard ANSI code. But that should probably only be done, when dealing with legacy applications.

zirconAuthor Commented:

Thank you!  Your answer is more-or-less what I expected to hear, but I wanted confirmation before I made the assumption.  My instructor's explanation was only cursory because we ran out of time.  The books I'm using as texts and references don't explain this issue very well either. I knew I could get the answer here. Thanks again.


Thanks for your comment. Clearly, casting is somewhat compiler dependent, since it works without the cast on my compiler and does not on my classmates' compilers. It looks as though both you and imladris agree that it is standard practice to cast calls to calloc.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.