c中的多维数组

时间:2015-01-11 11:15:05

标签: c arrays pointers multidimensional-array microcontroller

我在使用来自PHP背景的C中的多维数组时遇到问题。

我有一个数组msg,我现在用作输入。数组的第一个元素存储' node_id',第二个元素存储'实际消息'和第三个元素保持' controller_id'

任务由函数handleSlotMessage()处理:此函数应为每个controller_id接收3个输入消息(等待它接收所有3个消息),一旦接收到所有3个消息,则进一步传递以进行处理。

为了存储消息,我正在尝试使用多维数组' (这可以简单地用PHP实现):resultFromNodes但强调了我对C' C'我无法完成这件事。

对于handleSlotMessage函数中resultFromNodes变量的所有用法,我收到错误Subscribed value is not an array, pointer, or vector.

如何在' C' ?我尝试从this回答来检查数组是否已设置。

#define UNINITIALIZED 0xcdcdcdcd


void handleSlotMessage();

int main(int argc, const char * argv[]) {

    int resultFromNodes[99][3] = {UNINITIALIZED};
    int i, j;
    int msg[10];

    // test data only
    for ( i = 1; i < 4; i++ ) {
        for ( j = 1; j < 4; j++ ) {
            msg[0] = j; // sender's id (node id)
            msg[1] = i*j; // message
            msg[2] = i; // controller's id (requester's id)

            handleSlotMessage(msg, *resultFromNodes);

        }
    }
    return 0;
}


void handleSlotMessage(int msg[], int resultsFromNodes[]) {

    // get controller id
    int controller_id = msg[2];
    int node_id = msg[0];
    int message = msg[1];
    int slot_count = 3;

    // check if the resultFromNodes already has controller_id set
    if (resultsFromNodes[controller_id] == UNINITIALIZED) {
        resultsFromNodes[controller_id][0] = slot_count;
    }
    if(resultsFromNodes[controller_id][0] == 0) {
        printf("Message Handled and result from Nodes sent for further processing! \n");
    } else {
        resultsFromNodes[controller_id][node_id] = message;
        printf("on node: %i  msg: %i controller: %i  \n\r", node_id, message, controller_id);
        resultsFromNodes[controller_id][0]--;
    }

    return;
}

EDIT: If this gives any one better idea, 'controller_id' ranges from 1-99 so instead of checking it is set or not it is feasible to check if it is within that range. I don't know if it helps in any way.

3 个答案:

答案 0 :(得分:3)

您可能希望将您的功能定义为:

void handleSlotMessage(int msg[], int (*resultsFromNodes)[3]) { ... }

并将其称为:

handleSlotMessage(msg, resultFromNodes);

或同等的:

handleSlotMessage(msg, &resultFromNodes[0]);

答案 1 :(得分:2)

我指出其中一些问题不止一个问题

  1. 在函数handleSlotMessage内,参数resultsFromNodes应该声明为int *resultsFromNodes[4],以便能够发送类型为int [any number][4]的多维数组它。 声明就像:

    void handleSlotMessage(int msg[], int  resultsFromNodes[][4]){...}
    
  2. 调用您调用它的handleSlotMessage而不解除引用,如:

    handleSlotMessage(msg, resultFromNodes);
    
  3. 在PHP中使用函数内的resultFromNodes

    resultFromNodes[i][j] = 0;
    printf("%i", resultFromNodes[i][j]);
    
  4. 您无法将子集数组与数字进行比较或使用符号{1,2,3}进行比较:

    resultFromNodes[n] == 0xCDCDCDCD; // INVALID
    resultFromNodes[n] == {0xCDCDCDCD,0xCDCDCDCD,0xCDCDCDCD}; // INVALID
    

    相反,你必须自己比较每个元素,如:

    resultFromNodes[n][0] == 0xCDCDCDCD && resultFromNodes[n][1] == 0xCDCDCDCD && 
    resultFromNodes[n][2] == 0xCDCDCDCD && resultFromNodes[n][3] == 0xCDCDCDCD;
    
  5. 最后一件事是,在访问变量resultFromNodes的子集数组时,您的代码有溢出

    您将slot_count存储在resultFromNodes[controller_id][0] 计数器j代表node_id的值为1,2,3 因此4元素总共只有3个元素:

    resultFromNodes[controller_id][0] = slot_count;
    resultFromNodes[controller_id][1] = node_id;    // j=1
    resultFromNodes[controller_id][2] = node_id;    // j=2
    resultFromNodes[controller_id][3] = node_id;    // j=3
    

答案 2 :(得分:1)

        resultsFromNodes[controller_id][0] = slot_count;

resultsFromNodes被声明为一维数组,但是你给它第二个下标[0]