Casting calloc

Posted on 1998-03-06
Last Modified: 2012-08-14
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);
Question by:zircon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Author Comment

ID: 1257610
Edited text of question

Expert Comment

ID: 1257611
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.
LVL 16

Accepted Solution

imladris earned 50 total points
ID: 1257612
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.

LVL 16

Expert Comment

ID: 1257613
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.


Author Comment

ID: 1257614

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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question