I have a problem working with multi-dimensional array in C coming from PHP background.
I have an array msg which I am using as input for now. 1st element of array stores 'node_id', 2nd element stores 'actual message' and 3rd element holds 'controller_id'.
Task is handled by function handleSlotMessage(): This function should for each controller_id receive 3 input message (wait until it receives all 3 messages), upon receiving all 3 messages pass it further for processing.
For storing messages, I am trying to use a 'Multi-dimensional array' (which can be simply achieved in PHP): resultFromNodes
but underlining my limited knowledge of 'C' I am unable to get this done.
For all the usage of resultFromNodes variable in handleSlotMessage function I am getting an error Subscribed value is not an array, pointer, or vector.
How can this be implemented in 'C' ? I tried answer from this to check if array is set or not.
#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.
You probably wanted to define your function as:
void handleSlotMessage(int msg[], int (*resultsFromNodes)[3]) { ... }
and to call it as:
handleSlotMessage(msg, resultFromNodes);
or equivalent:
handleSlotMessage(msg, &resultFromNodes[0]);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments