943,915 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 553
  • C RSS
Jun 11th, 2009
0

Array question

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Jun 11th, 2009
0

Re: Array question

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 12th, 2009
0

Re: Array question

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()
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jun 12th, 2009
0

Re: Array question

>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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 12th, 2009
0

Re: Array question

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jun 12th, 2009
0

Re: Array question

>...if you want to be a pedant...
To make distinctions between static, automatic and dynamic attributes - is it too pedantic for you?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 12th, 2009
0

Re: Array question

>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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

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.
Message:
Previous Thread in C Forum Timeline: size of a file
Next Thread in C Forum Timeline: How can i more control rand()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC