我想從字符串中替換字符(例如第二個字符)。 我的代碼有什麼問題?它可以編譯,但不是做我所需要的,它給我一個分段錯誤。謝謝!通過替換字符串的字母在C中的分段錯誤
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main (int argc, string argv[])
{
string key="abcd";
key[1]='f';
}
並經過編譯我的代碼
~/workspace/pset2/crack/ $ clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow bug.c -lcrypt -lcs50 -lm -o bug
~/workspace/pset2/crack/ $ ./bug
Segmentation fault
'string'是從一個叫做'cs50'庫一個typedef,你得到它的權利 - 這只是'字符*'。顯然這個庫在哈佛的入門課程中用於從鍵盤讀取。這聽起來很有用,但是這個typedef實際上不應該存在。 – giusti
@giusti感謝您的信息。:) –