Interview Questions

How am I supposed to know to allocate things?

C Interview Questions and Answers


(Continued from previous question...)

How am I supposed to know to allocate things?

Q: But the man page for strcat says that it takes two char *'s as arguments. How am I supposed to know to allocate things?

A:In general, when using pointers you always have to consider memory allocation, if only to make sure that the compiler is doing it for you. If a library function's documentation does not explicitly mention allocation, it is usually the caller's problem.
The Synopsis section at the top of a Unix-style man page or in the ANSI C standard can be misleading. The code fragments presented there are closer to the function definitions used by an implementor than the invocations used by the caller. In particular, many functions which accept pointers (e.g. to structures or strings) are usually called with a pointer to some object (a structure, or an array-which the caller has allocated. Other common examples are time and stat.

(Continued on next question...)

Other Interview Questions