0

I am new to C, and I'm trying to populate a 2D array by breaking a text file of instructions into sub-strings and storing them into it. This loop is to populate said Array, within the while loop, it prints perfectly fine and seems to store it correctly. After the while loop, the 2D array is now empty and I do not understand why.

I should not have to allocate memory from the Heap as this is all within the same function correct? If I'm wrong, please let me know what I should be doing instead, thank you.

My goal is to have an instruction like

mul 2 4

be stored into

instructionArr[0][0] = mul 
instructionArr[0][1] = 2 
instructionArr[0][2] = 4

Is this the correct way to even go about this?

char instruction[256];
char* instructionArr[100][5];

int line = 0;

while(fgets(instruction, 256, fp) != NULL) {

    printf("%s", instruction);

    char* tokenized = strtok(instruction, " ");

    int count = 0;

    while (tokenized != NULL) {

        printf("tokenized: line: %d, position: %d is: %s\n", line, count, tokenized);

        instructionArr[line][count] = tokenized;

        //Prints string correctly
        printf("tokenizedArr[%d][%d] = %s\n", line, count, instructionArr[line][count]);

        tokenized = strtok(NULL, " ");

        count ++;
    }

    line ++;
}

//Print the first element of instructionArr, [0][0], should be a string, but is nothing.
printf("tokenizedArr[%d][%d] = %s\n", 0, 0, instructionArr[0][0]);
6
  • 1
    Tip: Before you footgun yourself, try and define constants like 256 somewhere so you can use them consistently, like #define BUFFER_SIZE 256 and then use that. Commented Nov 20, 2020 at 0:25
  • 1
    "the first element of instructionArr, [0][0], should be a string" In your own words, why? What is the type of instructionArr? Therefore, what is the type of instructionArr[0]? Therefore, what is the type of instructionArr[0][0]? Commented Nov 20, 2020 at 1:11
  • 1
    There are multiple logical errors here. It seems that you do not understand that the char type is not a "string" and in fact represents a single character. Or at least, if you do understand that, you haven't properly accounted for it. Commented Nov 20, 2020 at 1:13
  • The first element of insturctionArr[0][0] should be a string because that is what I have attempted to store into it. instructionArr is an array of char's correct? Is that a string in C? instructionArr is a 2D array of characters, oh... I think I see the error, I'm not storing 100x5 STRINGS, I'm storing 100x5 Characters, that is the issue here? Commented Nov 20, 2020 at 4:20
  • Ah I realized I had an error, it should be char* instructionArr[100][5]; not char instructionArr[100][5]; However, my issue remains Commented Nov 20, 2020 at 4:52

1 Answer 1

1

strtok modifies the string it operates on. See https://wiki.sei.cmu.edu/confluence/display/c/STR06-C.+Do+not+assume+that+strtok%28%29+leaves+the+parse+string+unchanged

Also you should probably use strcpy() instead of "instructionArr[line][count] = tokenized;"

EDIT: strncpy() is safer

Sign up to request clarification or add additional context in comments.

1 Comment

Simply changing to strcpy would not be a good idea as the left hand side currently does not point anywhere

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.