gpt4 book ai didi

c - 避免总线错误 : 10 during test cases

转载 作者:太空宇宙 更新时间:2023-11-04 07:56:38 24 4
gpt4 key购买 nike

我试图在不使用任何额外缓冲区的情况下从字符串中删除重复字符。当我像这样声明单个变量时,代码有效

char s[] = "aaabbb";

但当我试图循环通过一些测试用例时不是。

#include <stdio.h>
#include <string.h>

/* Design an algorithm and write code to remove the duplicate characters in a string
without using any additional buffer. NOTE: One or two additional variables are fine.
An extra copy of the array is not. */

void removeDuplicates(char s[]) {
// attempts to modify array in place without extra buffer
}

int main() {
char *s[4] = {"aaaa", "abcd", "ababab", "aaabbb"};

int i;
for (i = 0; i < 6; i++) {
removeDuplicates(s[i]);
}
return 0;
}

这会返回 Bus error: 10 因为它试图修改字符串文字 "aaaa" 但我不确定如何在保持良好设置的同时克服这个问题测试用例。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com