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.

int money(int deposit, ...){
  int 
      i,
      sum = 0,
      nextvar;
      // myArr[deposit]; // < -- in here i cant set the array size from function receiving argument? and why?
  va_list 
      parg;
  va_start(parg, deposit);
  for(i=0;i<deposit;i++){
    nextvar=va_arg(parg, int);
   // myArr[i]=nextvar;
    sum+=nextvar;
  }
  va_end(parg);
  return sum/deposit;
}
int main(void){
  printf("%d", money(4,4,4,4,4));
  getchar();
  return 0;
}

---
Andre Granovsky

Dani AI

Generated

The core issue is one of storage class and language version. In classic C (pre-C99) and in C++ an array size in a declaration must be a compile‑time constant; that is why int myArr[deposit]; inside your function would be rejected by those compilers. Since C99 the language allows variable‑length arrays (VLAs) so int myArr[deposit]; is legal there, but VLAs are automatic (stack) storage, not portable to every compiler (MSVC does not support them) and became optional in C11. was right that the original construct is not portable; correctly pointed out the distinction between automatic and static storage; and ’ note about compile‑time constants applies to pre‑C99 code.

Recommended patterns:

  • Prefer passing a pointer and a count to the function. This is portable, clear, and avoids stack surprises.
  • If you must allocate inside the function use malloc()/free() and handle allocation failures.
  • If you compile as C99 and accept its portability tradeoffs, a VLA (int buf[n];) is a concise option for moderate sizes.

Example (preferred, portable):

int average(const int *values, size_t n) {
    if (n == 0) return 0; /* choose error handling as appropriate */
    long sum = 0;
    for (size_t i = 0; i < n; ++i) sum += values[i];
    return (int)(sum / n);
}

Example (C99 VLA):

void fill_sequence(size_t n) {
    int buf[n]; /* VLA — not portable to all compilers */
    for (size_t i = 0; i < n; ++i) buf[i] = (int)i;
}

Practical tips: check for zero lengths, use size_t for counts, use a larger type for accumulation to avoid overflow, and avoid variadic lists for bulk numeric data (they are error‑prone because of type promotions). pointed to dynamic allocation resources earlier; if portability and safety matter most, pass an array + length or allocate with malloc() and document who frees it.

Recommended Answers

All 6 Replies

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()

>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.

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.

>...if you want to be a pedant...
To make distinctions between static, automatic and dynamic attributes - is it too pedantic for you?
;)

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.