If you're allocating memory for the 1st dimension only, then you only have one dimension. If you intend to have a 2D array (ie an array of strings) then you need to be allocating the second dimension as well (as you did in post #13).
If you're allocating memory for the 1st dimension only, then you only have one dimension. If you intend to have a 2D array (ie an array of strings) then you need to be allocating the second dimension as well (as you did in post #13).
calloc(4, sizeof(FILE*)) means that you have a dynamic array of 4 pointers to FILE, with the return value of calloc pointing to the first of the 4 pointers.Originally Posted by uz_mz
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You don't want to use calloc for pointer types, since it initialises them with all bits zero, which is a meaningless value for pointers. malloc would be a better choice and, if you need them initialised with null pointers, you could manually assign each element a null pointer.
Another thing to keep in mind is that your C implementation will have a limit for the number of FILE objects that may be open simultaneously, so you might consider redesigning your program to have few open at once, if that's a possibility.
But otherwise, you seem to be going about this correctly. You should check the return value of calloc/malloc and avoid casting their return values, as that will help you diagnose any errors that may occur. You can also use valgrind's memcheck tool if you have further problems.
In response to post #15, you should only free() something if it has been returned directly by malloc/calloc/realloc, or it's a null pointer. Similarly, you should only fclose() something if it's been opened with fopen/freopen or it's stdin/stdout/stderr. So, no, you would only call free() once in this case, but you may need to call fclose() up to four times.
i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!