二维阵列问题

时间:2011-08-08 05:32:34

标签: c arrays

我需要制作一个2D数组,其中一列存储某些结构和指针的指针。另一列存储一个32位幻数。 我怎么能在2D数组中做到这一点。 或任何其他方法来跟踪这两列信息?

2 个答案:

答案 0 :(得分:3)

您可以使用:

// The struct that will hold the pointer and the magic number
struct data {
    void *other_struct_ptr;
    unsigned int magic_number;
};

// Declare my array
struct data array[N];

其中N是数组的大小。现在只需将数据填入数组即可。例如:

array[0].other_struct_ptr = NULL; // I used NULL for simplicity
array[0].magic_number = 0xDEADC0DE;
array[1].other_struct_ptr = NULL;
array[1].magic_number = 0xCAFEBABE;

答案 1 :(得分:1)

定义这样的结构:

struct data_t
{
    void *pointer;
    int magic_number;
};

然后使用以下数组:

data_t values[100]; //100 is just for example

或许您需要这样的2D阵列:

data_t values[100][100]; //100s are just for example
相关问题