For my C class, I have to write a program where the user enters how many fahrenheits he will be entering, but the fahrenheits have to be in an array. So a dynamic array seems the obvious choice. In main, we can only have initializations and function calls.
The function itself doesn't care whether you have a real array or a pointer to allocated memory, the function is declared and written in exactly the same way.
>I understand that, but the user has to enter the size of the array, and in a different function than main.
Then use a pointer to modify the memory in the variables in main(). Or if you can, return the value that the user entered in the function.
>Also, what would the receiving functions look like in your example?
They'd look exactly the same as if they had been written for static arrays. One thing you have to remember though, is to always pass the size of the dynamic array to the function (even though it's not a requirement), because otherwise you run the risk of overrunning the array boundaries.
>how to have the user decide the size of the array through another function.
What's so hard about that? Just get the user to input a number, and return that value back. (although you might want to validate the number first, so as to avoid negative array sizes...)
You don't even necessarily have to have the user choose the size of the array...
you could let the program change the size of the array on the fly...ie. if the user enters another value that is beyond the size of the current array, the program could double the size variable, create a new array with that size, copy the old values from the old array, add the new value entered by the user, then reassign the new array to the variable of the old array...it sounds complicated, but it's not too bad...
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.