0

I have this array of pointers to strings which is defined as public above the Main function:

char *Code[]={"MAIN:  add r3,  LIST",
            "LOOP: prn #48",
            "lea STR, r6",
            "inc r6",
            "mov r3, K",
            "sub r1, r4",
            "bne END",
            "cmp val1, #-6",
            "bne %END",
            "dec K",
            "jmp %LOOP",
            "END: stop",
            "STR: .string “abcd”",
            "LIST: .data 6, -9",
            ".data -100",
            ".entry K",
            "K: .data 31"};

I wrote A function that suppose to find if there is A sequence of tabs and spaces in A row and if there are it should change the string so there will be only one space or tab:

void SpaceTabRemover()//function to make sure there are no two tabs or spaces following eachother
{
    int i,j=0,k=0; //i=which line we are at, j=which char of the line we ar at, k=placeholder for the last char which equal to space or tab
    for(i=0;i<sizeof(Code)/sizeof(Code[0]);i++)
    {
        while(Code[i][j])
        {
            if ((Code[i][j]==' '||Code[i][j]=='\t')&&(Code[i][j+1]==' '||Code[i][j+1]=='\t'))//checks if there is a sequence of tabs and spaces
            {
                Code[j][k++]=Code[i][j];
            }
            j++;
        }
        Code[j][k]='\0';
    }
}

I think the code should work by the look of it, unless I wrote something wrong and I can not see it,

My problem here is the moment the function does find two spaces or tabs in A row and try to make the change to the string I get an error I have never seen before:

Program recieved signal SIGSEGV segmentation fault in SpaceTabRemover() at main.c.112 Code[j][k++]=Code[i][j]

What does this error means and how do I fix it?

Roy Shiff
  • 63
  • 7

1 Answers1

1

The elements of the arrya Code are pointesr to string literals, which are not allowed to be modified.

You have to copy the strings to modifiable buffer before modifying strings.

#include <stdlib.h> // for malloc() and exit()
#include <string.h> // for strlen() and strcpy()

void SpaceTabRemover()//function to make sure there are no two tabs or spaces following eachother
{
    int i,j=0,k=0; //i=which line we are at, j=which char of the line we ar at, k=placeholder for the last char which equal to space or tab
    for(i=0;i<sizeof(Code)/sizeof(Code[0]);i++)
    {
        // allocate buffer and copy strings
        char* buffer = malloc(strlen(Code[i]) + 1); // +1 for terminating null-character
        if (buffer == NULL) exit(1); // check if allocation succeeded
        strcpy(buffer, Code[i]); // copy the string
        Code[i] = buffer; // assign the copied string to the element of array

        while(Code[i][j])
        {
            if ((Code[i][j]==' '||Code[i][j]=='\t')&&(Code[i][j+1]==' '||Code[i][j+1]=='\t'))//checks if there is a sequence of tabs and spaces
            {
                Code[j][k++]=Code[i][j];
            }
            j++;
        }
        Code[j][k]='\0';
    }
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70