How to pass dynamic arrays?

Reply

Join Date: Jul 2005
Posts: 55
Reputation: shmay is an unknown quantity at this point 
Solved Threads: 0
shmay shmay is offline Offline
Junior Poster in Training

How to pass dynamic arrays?

 
0
  #1
Apr 1st, 2007
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:

  1. int main (void)
  2. {
  3. 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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: How to pass dynamic arrays?

 
0
  #2
Apr 1st, 2007
pass the pointer and the length of the array.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to pass dynamic arrays?

 
0
  #3
Apr 1st, 2007
  1. int arr[10];
  2. int *pArr = malloc ( 10 * sizeof *pArr );
  3. myFunc( arr );
  4. myFunc( pArr );
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: shmay is an unknown quantity at this point 
Solved Threads: 0
shmay shmay is offline Offline
Junior Poster in Training

Re: How to pass dynamic arrays?

 
0
  #4
Apr 3rd, 2007
I understand that, but the user has to enter the size of the array, and in a different function than main.

Also, what would the receiving functions look like in your example? Thanks!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: How to pass dynamic arrays?

 
0
  #5
Apr 3rd, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: shmay is an unknown quantity at this point 
Solved Threads: 0
shmay shmay is offline Offline
Junior Poster in Training

Re: How to pass dynamic arrays?

 
0
  #6
Apr 3rd, 2007
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.

  1. {
  2. int *pArr, Size;
  3.  
  4. Size = /* Function call here*/
  5. myFunc( pArr, Size );
  6. }
  7.  
  8. /* Put function that returns size here*/
  9.  
  10. void myFunc(int dArr[], int Size)
  11. {
  12. dArr = malloc (sizeof (int) * Size);
  13. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: How to pass dynamic arrays?

 
0
  #7
Apr 3rd, 2007
>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...)
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: How to pass dynamic arrays?

 
0
  #8
Apr 4th, 2007
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC