Interview Questions

How can I declare local arrays of a size matching a passed-in array?

C Interview Questions and Answers


(Continued from previous question...)

How can I declare local arrays of a size matching a passed-in array?

Until recently, you couldn't; array dimensions in C traditionally had to be compile-time constants. However, C99 introduces variable-length arrays (VLA's) which solve this problem; local arrays may have sizes set by variables or other expressions, perhaps involving function parameters. (gcc has provided parameterized arrays as an extension for some time.) If you can't use C99 or gcc, you'll have to use malloc, and remember to call free before the function returns.

(Continued on next question...)

Other Interview Questions