我尝试将数组项之一设置为特定值。该程序已编译,但当我执行时返回段错误。
这是代码:
#include <stdio.h>
void Debug(unsigned char* str, char simbol, char size);
int main() {
Debug((unsigned char*)"DEBUG: message: x\r\n", 'e', 40);
return(0);
}
//this function I try to replace all occurrences of x with simbol value 'e'
void Debug (unsigned char* str, char simbol, char size){
char i;
for (i = 0 ; i < size ; i++){
if( str[i] == 'x' ){
str[i] = simbol;
}
}
printf ("%s\n", str);
}
预先感谢您的帮助!
最佳答案
unsigned char* str
此处 str 是只读的,您正尝试写入此位置,因此出现段错误。
有一个数组
unsigned char str[100] = "DEBUG: message: x\r\n";
并将该数组传递给函数。
关于无法设置 C char 数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28495527/