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

C语言中如何通过数组指针获取数组长度?含函数传参场景

Alright, let's tackle these two C language questions head-on—they're super common for folks working with arrays and pointers, so let's make this clear with practical examples.

1. 如何通过数组指针确定数组的长度?

First, let's clarify: an array pointer (like int (*p)[5]) is a pointer that points to an entire array, not just a single element. If your array pointer is typed to point to a fixed-size array, you can calculate the length using the sizeof operator, just like you would with a regular array.

Here's the breakdown:

  • sizeof(*p) gives you the total number of bytes occupied by the array the pointer is pointing to.
  • Divide that by the size of a single element (sizeof((*p)[0])) to get the number of elements in the array.

Example code:

#include <stdio.h>

int main() {
    int my_array[5] = {10, 20, 30, 40, 50};
    int (*array_ptr)[5] = &my_array; // Pointer to the entire int[5] array

    size_t array_length = sizeof(*array_ptr) / sizeof((*array_ptr)[0]);
    printf("Array length: %zu\n", array_length); // Outputs 5
    return 0;
}

Important note: This only works if the array pointer's type explicitly includes the array size. If you have a generic pointer like int ** (which is a pointer to a pointer, not an array pointer), you can't use this method—you've lost the array size information at that point.

2. 在C语言中,将数组指针作为函数参数传入时,如何获取该数组的长度?

This depends on how you define your function parameter—let's cover the two main scenarios:

Case 1: Function accepts a pointer to a fixed-size array

If your function's parameter is declared to point to a specific-sized array (e.g., int (*arr_ptr)[5]), the compiler still knows the size of the array being pointed to. You can use the same sizeof trick inside the function.

Example:

#include <stdio.h>

void get_array_length(int (*arr_ptr)[5]) {
    size_t len = sizeof(*arr_ptr) / sizeof((*arr_ptr)[0]);
    printf("Length inside function: %zu\n", len); // Outputs 5
}

int main() {
    int my_array[5] = {1, 2, 3, 4, 5};
    get_array_length(&my_array); // Pass the address of the array (array pointer)
    return 0;
}

Case 2: Function accepts a generic array pointer (unknown size)

If your function needs to handle arrays of varying sizes, you can't rely on the array pointer alone to get the length. The compiler can't infer the array size from a parameter like int (*arr_ptr)[]—this is an incomplete type, and sizeof(*arr_ptr) will throw a compile error.

In this scenario, the standard and safest approach is to pass the array length as a separate function parameter.

Example:

#include <stdio.h>

void process_array(int (*arr_ptr)[], size_t array_len) {
    printf("Received array length: %zu\n", array_len);
    // Access elements like: (*arr_ptr)[index] where index < array_len
}

int main() {
    int my_array[7] = {1, 2, 3, 4, 5, 6, 7};
    size_t len = sizeof(my_array) / sizeof(my_array[0]);
    process_array(&my_array, len);
    return 0;
}

Quick pitfall to avoid: Don't confuse array pointers with element pointers. If you pass an array name directly to a function (e.g., process_array(my_array)), the array name decays to a pointer to its first element (int *), not an array pointer. In that case, sizeof(arr) inside the function will give you the size of a pointer (4 or 8 bytes, depending on your system), not the array—so always pass the length explicitly if you're using element pointers.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:02:10