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

能否通过C宏将数组下标替换为哈希函数调用?老代码改造需求

Replace Array Indices with Hash Calls via C Macros (No Code Modifications)

Got it, let's tackle this problem. You want to replace every instance of array[idx] with array[hash(idx)] without touching your existing usage code, and have the hash function return a valid index between 0 and 999. Here are two practical solutions depending on your constraints:

Solution 1: Standard C Compatible (Minimal Array Definition Change)

This approach only requires modifying the array's declaration (not any of the code that uses it) and works with all standard C compilers.

First, define your hash function to ensure it always returns a valid index (even for negative inputs):

#include <stdio.h>

// Hash function: returns 0-999 for any integer input
int hash(int idx) {
    // Convert negative values to unsigned to avoid negative modulo results
    unsigned int unsigned_idx = (unsigned int)idx;
    return unsigned_idx % 1000;
}

// Rename the actual array to an internal name
int _array[1000];

// Macros to rewrite array[idx] as _array[hash(idx)]
#define array _array[hash(
#define ] )]

// Your original main code stays completely unchanged
int main() { 
    int input; 
    scanf("%d", &input); 
    array[input] = 1; // Expands to _array[hash(input)] = 1;
    return 0; 
}

How it works:

  • The array macro replaces the original array name with _array[hash(.
  • The ] macro replaces the closing bracket with )].
  • When the preprocessor runs, array[input] gets expanded to _array[hash(input)] automatically.

Solution 2: No Array Definition Changes (Compiler Extension Required)

If you can't modify the original array declaration at all, you can use a GCC/clang extension that allows using operators as macro names. This will rewrite all array index accesses (not just array) to use the hash function:

#include <stdio.h>

// Hash function same as above
int hash(int idx) {
    unsigned int unsigned_idx = (unsigned int)idx;
    return unsigned_idx % 1000;
}

// Macros to replace [x] with [hash(x)] globally
#define [ [hash(
#define ] )]

// Your original code is 100% unchanged
int array[1000]; 
int main() { 
    int input; 
    scanf("%d", &input); 
    array[input] = 1; // Expands to array[hash(input)] = 1;
    return 0; 
}

Note:

  • This uses a non-standard preprocessor feature supported by GCC, clang, and other modern compilers, but won't work with strict standard C compilers.
  • Every array access in your code will use the hash function, so make sure that's what you want.

Key Notes on the Hash Function:

  • We cast the input to unsigned int first to handle negative values correctly (since negative modulo in C can return negative results, which would be invalid array indices).
  • Using % 1000 ensures the result is always in the 0-999 range, matching your array's size.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:18:38