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?

5
  • You are trying to modify string linterals, but it is not allowed. Commented Feb 23, 2021 at 13:16
  • so there is no way to remove tabs and spaces from the strings? Commented Feb 23, 2021 at 13:18
  • 1
    Yes. If you want to modify strings, you have to copy them to modifiable buffer before that. Commented Feb 23, 2021 at 13:19
  • is it possible to copy it back into the array after i changed it? Commented Feb 23, 2021 at 13:25
  • Yes, the elements of the array Code are pointers and you can assign the pointers to copied strings to the elements. Commented Feb 23, 2021 at 13:26

1 Answer 1

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

Comments

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.