| | |
How to pass dynamic arrays?
![]() |
•
•
Join Date: Jul 2005
Posts: 55
Reputation:
Solved Threads: 0
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.
For example, if i have:
How would I pass the dynamic array to another function? Do I have to use malloc in only the first function that I call it to? Help!
For example, if i have:
C Syntax (Toggle Plain Text)
int main (void) { int *dynArray;
How would I pass the dynamic array to another function? Do I have to use malloc in only the first function that I call it to? Help!
C Syntax (Toggle Plain Text)
int arr[10]; int *pArr = malloc ( 10 * sizeof *pArr ); myFunc( arr ); myFunc( pArr );
>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.
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.
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Jul 2005
Posts: 55
Reputation:
Solved Threads: 0
Ah, I see. Thanks. I'm still left with one problem I can't solve though: how to have the user decide the size of the array through another function.
That's my shitty attempt at it. I have tried many other variations. Which is the correct way?
C Syntax (Toggle Plain Text)
{ int *pArr, Size; Size = /* Function call here*/ myFunc( pArr, Size ); } /* Put function that returns size here*/ void myFunc(int dArr[], int Size) { dArr = malloc (sizeof (int) * Size); }
That's my shitty attempt at it. I have tried many other variations. Which is the correct way?
Last edited by shmay; Apr 3rd, 2007 at 9:29 pm.
>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...)
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...)
"Technological progress is like an axe in the hands of a pathological criminal."
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...
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...
![]() |
Similar Threads
- Creating dynamic arrays (C++)
Other Threads in the C Forum
- Previous Thread: Error declaring class, pls help
- Next Thread: linked list problem!!!
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest match matrix meter microsoft number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






