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

如何无需循环将数组元素地址复制到指针数组以简化代码?

Simplifying the Controller Pointer Array Initialization

Great question! Let's break down why a direct memcpy-style approach won't work first, then look at cleaner alternatives to your loop.

First, the problem with memcpy: Your controller array stores pointers to each element of arr. These pointers (&arr[0], &arr[1], &arr[2]) are spaced by sizeof(int) bytes (since each arr element is an int), but controller expects elements spaced by sizeof(int*) bytes. There's no single contiguous block of memory that holds exactly these pointer values ready to copy—so memcpy isn't a fit here.

But we have several cleaner ways to initialize controller without a verbose loop:

1. Direct Initialization List (Best for Fixed-Small Arrays)

If your array size is fixed (like 3 in your example), you can initialize controller directly when you declare it. This eliminates the loop entirely:

int arr[3] = {3,4,5};
int* controller[3] = { &arr[0], &arr[1], &arr[2] };
// Or even shorter, since arr decays to &arr[0]:
// int* controller[3] = { arr, arr+1, arr+2 };

This is the most readable option for small, fixed-size arrays.

2. Simplified Loop (For Dynamic/Large Arrays)

If your array size is variable (e.g., defined by a macro or runtime value), you can shorten the loop to be more concise. Instead of indexing arr each time, use a pointer to iterate:

int* ptr = arr;
for (int idx = 0; idx < 3; idx++) {
    controller[idx] = ptr++;
}

This cuts down on redundant array indexing and feels cleaner than your original loop.

3. Compile-Time Macro Expansion (For Known Sizes)

If you're working with arrays where the size is known at compile time, you can use a preprocessor macro to generate the initialization list automatically. This avoids writing out each element manually for larger arrays:

#define INIT_CONTROLLER(N, arr_name, ctrl_name) \
    int* ctrl_name[N] = { \
        MACRO_REPEAT(N, arr_name) \
    }

#define MACRO_REPEAT(N, arr) \
    MACRO_REPEAT_##N(arr)

#define MACRO_REPEAT_1(arr) arr
#define MACRO_REPEAT_2(arr) arr, arr+1
#define MACRO_REPEAT_3(arr) arr, arr+1, arr+2
// Add more for larger sizes as needed

// Usage:
int arr[3] = {3,4,5};
INIT_CONTROLLER(3, arr, controller);

Note that this requires defining a macro for each possible array size you might use, which is less flexible but works well if you have a set of common sizes.

Bonus: Using Compound Literals (C99+)

If you want to avoid declaring controller separately or need it inline, you can use a compound literal to create the pointer array on the fly when calling changearr:

changearr((int*[]){ arr, arr+1, arr+2 });

This lets you skip initializing a separate controller variable entirely, which might be even cleaner for your use case!

All these approaches will work with your existing changearr function, since they correctly populate the pointer array with addresses of arr elements.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 18:35:27