更改数组中的 A 字符串时出现 C 分段错误
C segmentation error when changing A string in array
我有这个指向字符串的指针数组,在 Main 函数上方定义为 public:
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"};
我写了一个函数,假设在一行中查找是否有一系列制表符和 spaces,如果有,它应该更改字符串,以便只有一个 space或选项卡:
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]='[=11=]';
}
}
我认为代码看起来应该可以工作,除非我写错了并且看不到它,
我的问题是当函数确实在一行中找到两个 space 或制表符并尝试更改字符串时,我遇到了一个以前从未见过的错误:
Program recieved signal SIGSEGV segmentation fault in SpaceTabRemover() at main.c.112 Code[j][k++]=Code[i][j]
这个错误是什么意思,我该如何解决?
arrya Code
的元素是指向字符串文字的指针,不允许修改。
您必须在修改字符串之前将字符串复制到可修改缓冲区。
#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]='[=10=]';
}
}
我有这个指向字符串的指针数组,在 Main 函数上方定义为 public:
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"};
我写了一个函数,假设在一行中查找是否有一系列制表符和 spaces,如果有,它应该更改字符串,以便只有一个 space或选项卡:
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]='[=11=]';
}
}
我认为代码看起来应该可以工作,除非我写错了并且看不到它,
我的问题是当函数确实在一行中找到两个 space 或制表符并尝试更改字符串时,我遇到了一个以前从未见过的错误:
Program recieved signal SIGSEGV segmentation fault in SpaceTabRemover() at main.c.112 Code[j][k++]=Code[i][j]
这个错误是什么意思,我该如何解决?
arrya Code
的元素是指向字符串文字的指针,不允许修改。
您必须在修改字符串之前将字符串复制到可修改缓冲区。
#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]='[=10=]';
}
}