0

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.
6
  • 2
    This int resultFromNodes[99][3] = {UNINITIALIZED}; most probably does not do what you might expect. It inits resultFromNodes[0][0] with 0xcdcdcdcd and all other elements with 0. Commented Jan 11, 2015 at 11:21
  • @alk all other element being assigned 0, since I could check if it is 0 or not as well. but the problem is, I cannot assign values to resultsFromNodes[controller_id][0], [1] or any other array, simple assignment shows an error. Commented Jan 11, 2015 at 11:30
  • 1
    In C reading an uninitialised variable invokes undefined behaviour. Do not do that. Commented Jan 11, 2015 at 11:32
  • Thanks, got the idea. I can intialize all array as 0 an carry on, it seems problem was with my function definition, where I used 1-D array for resultFromNodes Commented Jan 11, 2015 at 11:45
  • 1
    You're treating resultsFromNodes in handleSlotMessage where it is a declared parameter of type int[], as if it were the same declaration in main(). Commented Jan 11, 2015 at 11:47

3 Answers 3

3

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]);
Sign up to request clarification or add additional context in comments.

Comments

2

there is more than one problem I point some of them

  1. Inside the function handleSlotMessage the argument resultsFromNodes should be declared like int *resultsFromNodes[4] in order to be able to send a multi-dimensional arrays of type int [any number][4] to it. the declaration would be like:

    void handleSlotMessage(int msg[], int  resultsFromNodes[][4]){...}
    
  2. to call the handleSlotMessage you call it without dereferencing just like:

    handleSlotMessage(msg, resultFromNodes);
    
  3. to use the resultFromNodes inside the function just like PHP:

    resultFromNodes[i][j] = 0;
    printf("%i", resultFromNodes[i][j]);
    
  4. you can't compare subset array with a number or using the notation {1,2,3} like:

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

    Instead you have to compare each element on its own like:

    resultFromNodes[n][0] == 0xCDCDCDCD && resultFromNodes[n][1] == 0xCDCDCDCD && 
    resultFromNodes[n][2] == 0xCDCDCDCD && resultFromNodes[n][3] == 0xCDCDCDCD;
    

the last thing is that your code has an overflow when accessing the subset arrays of the variable resultFromNodes

you are storing the slot_count at resultFromNodes[controller_id][0] and the counter j represents node_id will have the values 1,2,3 hence 4 elements in total you have only 3 element:

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 Comments

Thanks, I realized all of the error and fixed it, since I am new to c, I am real sloppy. Well thank you once again.
also regarding your function declaration, I don't know why but it seems *resultsFromNodes needs to be within () int (*resultsFromNodes) [4]){...}
1
        resultsFromNodes[controller_id][0] = slot_count;

resultsFromNodes is declared as a 1-D array, but you give it a second subscript [0].

1 Comment

Thanks, it seems I had this error, resultFromNodes on function definition declared as 1-D array and few other mistake that I realized from other answer and comment. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.