[英]Reversing a string using recursion - getting 'Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)' error
I'm trying to reverse a string with the following code but am getting an error Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
.我正在尝试使用以下代码反转字符串,但出现错误
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
。
I understand that it's telling me that I'm trying to access a NULL pointer but I fail to see where:我知道它告诉我我正在尝试访问 NULL 指针,但我看不到在哪里:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int empty(char s[]){
return strcmp(s, "") == 0 ? 1 : 0;
}
char getHead(char s[]){
char *dup = (char *)malloc(sizeof(char));
strcpy(dup, (s + 0));
return *dup;
}
char * getTail(char s[]){
char *dup = malloc(sizeof(char) * strlen(s) - 1);
for(int i = 0; i < strlen(s) - 1; i++){
dup[i] = s[i+1];
}
return dup;
}
char * ReverseStringHeadTail(char s[]){
if(empty(s))
{
return NULL;
}
else
{
char head = getHead(s);
char *tail = getTail(s);
return strcat(ReverseStringHeadTail(tail), head); // Error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
}
}
int main(int argc, const char * argv[]) {
char string[] = "string";
printf("%s\n", ReverseStringHeadTail(string));
return 0;
}
Thanks!谢谢!
The function empty
can be written simpler.函数
empty
可以写得更简单。
int empty( const char *s )
{
return *s == '\0';
}
The function getHead
is invalid and does not make sense.函数
getHead
无效且没有意义。 Its allocates memory for one character but is trying to copy a whole string.它为一个字符分配内存,但试图复制整个字符串。 And its call results in a memory leak.
它的调用会导致内存泄漏。
You can just write你可以写
char getHead( const char *s )
{
return *s;
}
The function getTail
is also invalid.函数
getTail
也无效。 For example if the string is for example "A"
then the function allocates a memory of 0 bytes.例如,如果字符串是例如
"A"
则该函数分配 0 字节的内存。 In any case the allocated memory will not contain a string.在任何情况下,分配的内存都不会包含字符串。
The function strcat
may not be used with an object of the type char or with a null-pointer.函数
strcat
不能与 char 类型的对象或空指针一起使用。
It seems what you mean is the following.看来你的意思是以下内容。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * ReverseStringHeadTail( const char *s )
{
if ( *s == '\0' ) return calloc( 1, 1 );
char tmp[2] = { *s, '\0' };
char *p = ReverseStringHeadTail( s + 1 );
p = realloc( p, strlen( p ) + sizeof( tmp ) );
return strcat( p, tmp );
}
int main(void)
{
const char *s = "12345";
puts( s );
char *p = ReverseStringHeadTail( s );
puts( p );
free( p );
return 0;
}
The program output is the following程序输出如下
12345
54321
Pay attention to that the function does not check whether the memory was allocated successfully.注意该函数不会检查内存是否分配成功。
char *revm(char *buff, const char *str, const size_t len)
{
if(!len) len = strlen(str);
if(!buff)
{
buff = calloc(len + 1, 1);
}
if(buff && len)
{
*buff = *(str + len - 1);
*(buff + len - 1) = *str;
if(len > 2) rev(buff + 1, str + 1, len - 2);
}
return buff;
}
int main(void)
{
char *str = "1234567890";
char *str1 = rev(NULL, str, 0);
printf("%s\n", str1);
return EXIT_SUCCESS;
}
or if you do not want to reverse the string given (it has to be writable)或者如果您不想反转给定的字符串(它必须是可写的)
char *rev(char *str, size_t len)
{
if(!len) len = strlen(str);
if(len)
{
int tmp = *str;
*str = *(str + len - 1);
*(str + len - 1) = tmp;
if(len > 2) rev(str + 1, len - 2);
}
return str;
}
int main(void)
{
char str[] = "123456789";
char *str1 = rev(str, 0);
printf("%s\n", str1);
return EXIT_SUCCESS;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.