You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言新手咨询:a[]是否为固定大小数组?

Is a[] a Fixed-Size Array in C?

Hey there! Great question—this is one of those subtle C language details that catches a lot of new learners off guard. The short answer is: it depends on where you use a[]! Let’s break down the two key scenarios clearly:

1. a[] in Variable Declarations (Global/Local Scope)

When you use a[] to declare an array with an initializer list (either in global scope or inside a function), this is just a handy shorthand for a fixed-size array. The compiler automatically calculates the array’s exact size based on the number of elements in your initializer.

For example:

// Global scope: compiler counts 3 elements, so a becomes int[3]
int a[] = {1, 2, 3};

void my_func() {
    // Local scope: compiler counts 2 elements, so b becomes int[2]
    int b[] = {10, 20};
}

Once the size is set at compile time, this array is fixed-size—you can’t resize it later, just like if you’d written int a[3]; explicitly. It takes up a fixed block of memory (either in the data segment for global arrays or on the stack for local ones).

2. a[] in Function Parameters

This is where things get tricky. When you see a[] as a function parameter, it’s actually syntax sugar for a pointer. C doesn’t let you pass entire arrays to functions directly—instead, it passes a pointer to the array’s first element.

These two function declarations are completely identical:

void process_array(int a[]);
void process_array(int *a);

In this case, a[] does not represent a fixed-size array. You can pass any size of int array to this function, and the parameter a will just point to the start of that array. The function has no built-in way to know the original array’s size (which is why passing the size as a separate parameter is such a common practice).

Quick Recap

  • In variable declarations with an initializer: a[] creates a fixed-size array (size determined by the initializer).
  • In function parameters: a[] is just a pointer, not a fixed-size array.

内容的提问来源于stack exchange,提问作者moh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:49:21