英文:
reverse a string using pointers (have to use unsigned *char) in C
问题
以下是您提供的代码的翻译部分:
我目前正在为大学的作业而工作。
任务是在一个单独的函数中反转一个字符串。函数名如下所示:
> `void string_rev(unsigned char *str);`
我的解决方案如下:
```c
void string_rev(unsigned char *str){
int length = 0;
int counter = 0;
unsigned char *endptr = str;
for (int i = 0; str[i] != '我目前正在为大学的作业而工作。
任务是在一个单独的函数中反转一个字符串。函数名如下所示:
> `void string_rev(unsigned char *str);`
我的解决方案如下:
```c
void string_rev(unsigned char *str){
int length = 0;
int counter = 0;
unsigned char *endptr = str;
for (int i = 0; str[i] != '\0'; i++) //获取字符串的长度
length++;
for (int i = 0; i < length; i++) //将endptr放到字符串的末尾
endptr++;
while (counter < length/2){ //交换从开头到末尾的值,直到字符串的一半
char temp = *str;
*str = *endptr;
*endptr = temp;
str++;
endptr--;
counter++;
}
for (int i = 0; i<length; i++){
printf("%c", *str);
str++;
}
}
int main (void){
char *array = "Hello";
unsigned char *ptr = (unsigned char*)array;
string_rev(ptr);
return 0;
}
'; i++) //获取字符串的长度
length++;
for (int i = 0; i < length; i++) //将endptr放到字符串的末尾
endptr++;
while (counter < length/2){ //交换从开头到末尾的值,直到字符串的一半
char temp = *str;
*str = *endptr;
*endptr = temp;
str++;
endptr--;
counter++;
}
for (int i = 0; i<length; i++){
printf("%c", *str);
str++;
}
}
int main (void){
char *array = "Hello";
unsigned char *ptr = (unsigned char*)array;
string_rev(ptr);
return 0;
}
我收到的错误是“总线错误10!但我找不到错误。
这可能与unsigned char*
有关,但我无法让它工作。有人可以帮忙吗?
FYI -> 我们必须使用unsigned char*
!当然要使用指针!
谢谢
<details>
<summary>英文:</summary>
I am currently working on an assignment for Uni.
The task is to reverse a string in a separate function. The function-name is given like that:
> `void string_rev(unsigned char *str);`
My Solution looks like this:
void string_rev(unsigned char *str){
int length = 0;
int counter = 0;
unsigned char *endptr = str;
for (int i = 0; str[i] != 'int length = 0;
int counter = 0;
unsigned char *endptr = str;
for (int i = 0; str[i] != '\0'; i++) //getting length of string
length++;
for (int i = 0; i < length; i++) //putting endptr to end of string
endptr++;
while (counter < length/2){ //switch values from start to end until half of string
char temp = *str;
*str = *endptr;
*endptr = temp;
str++;
endptr--;
counter++;
}
for (int i = 0; i<length; i++){
printf("%c", *str);
str++;
}
'; i++) //getting length of string
length++;
for (int i = 0; i < length; i++) //putting endptr to end of string
endptr++;
while (counter < length/2){ //switch values from start to end until half of string
char temp = *str;
*str = *endptr;
*endptr = temp;
str++;
endptr--;
counter++;
}
for (int i = 0; i<length; i++){
printf("%c", *str);
str++;
}
}
int main (void){
char *array = "Hello";
unsigned char *ptr = (unsigned char*)array;
string_rev(ptr);
return 0;
}
The Error I get is Bus Error 10! But I can't find the mistake.
It may has to do with the unsigned char* but I don't get it to work. Can someone please help?
FYI -> We have to use unsigned char*! And of course pointers!
Thank you :)
</details>
# 答案1
**得分**: 5
请用以下方式替换:
```cpp
char array[] = "Hello";
英文:
You are trying to modify a string constant. Replace
char *array = "Hello"; // Not an array.
with
char array[] = "Hello";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论