Array question

Thread Solved

Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Array question

 
0
  #1
Jun 11th, 2009
Hello.,
Have a question about array being set from function argument. I wrote this little program, and want to know why cant i set an array size from the function argument. Any help would be greatly appreciated.

  1. int money(int deposit, ...){
  2. int
  3. i,
  4. sum = 0,
  5. nextvar;
  6. // myArr[deposit]; // < -- in here i cant set the array size from function receiving argument? and why?
  7. va_list
  8. parg;
  9. va_start(parg, deposit);
  10. for(i=0;i<deposit;i++){
  11. nextvar=va_arg(parg, int);
  12. // myArr[i]=nextvar;
  13. sum+=nextvar;
  14. }
  15. va_end(parg);
  16. return sum/deposit;
  17. }
  18. int main(void){
  19. printf("%d", money(4,4,4,4,4));
  20. getchar();
  21. return 0;
  22. }


---
Andre Granovsky
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,340
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array question

 
0
  #2
Jun 11th, 2009
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Array question

 
0
  #3
Jun 12th, 2009
short answer: static arrays are allocated when the function is called, at which point your variable is undefined. so the construct is illegal.

if you want to dynamically allocate arrays, you need to use malloc() along with a corresponding free()
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Array question

 
0
  #4
Jun 12th, 2009
>short answer: static arrays are allocated when the function is called...
Short remark: static arrays are allocated before the first call of the function. However no static arrays in OP snippet. Please, be more thoroughs in terms using.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Array question

 
0
  #5
Jun 12th, 2009
uh, yeah, i meant to say that the memory is allocated when the "program" is called.

but look if you want to be a pedant, then it's true there's no static array in the OP's snippet. what there is is an illegal attempt to use an undefined variable in what would otherwise have been a static array.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Array question

 
0
  #6
Jun 12th, 2009
>...if you want to be a pedant...
To make distinctions between static, automatic and dynamic attributes - is it too pedantic for you?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Array question

 
0
  #7
Jun 12th, 2009
>static arrays are ............
You meant to say "fixed-length" arrays, right?

OP>in here i cant set the array size from function receiving argument? and why?
Because, array size should be a compile time constant, that is: the array size should be known at the compile time. You are already been told what is the solution by Dave.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC