[英]Reversing a C string function crashes?
I'm trying to write a C function to reverse a passed in C style string (ie char *) and return the char pointer of the reversed string. 我正在尝试编写一个C函数来反转传入的C样式字符串(即char *)并返回反转字符串的char指针。 But when I run this in VS2012, nothing is printed in terminal and "main.exe has stopped working" msg shows up.
但是,当我在VS2012中运行此程序时,终端上未打印任何内容,并且显示“ main.exe已停止工作”。
#include <stdio.h>
#include <string.h>
char * rrev_str(char * str )
{
char *revd_str=""; //I tried char revd_str []="" error: stack around "revd_str" is corrupted
int i,r;
int str_len=strlen(str);
for (i = str_len-1, r=0; i >=0; i--,r++)
{
revd_str[r]= str[i];
}
return revd_str;
}
int main(int argc, char* argv[])
{
char str1 [] ="STEETS";
char str2 [] ="smile everyday!";
//reverse "chars" in a C string and return it
char * rev_string=rrev_str(str1);
}
The problem here is three fold. 这里的问题有三方面。 First you aren't allocating enough space for the reversed string, and secondly you are returning a pointer to a local variable in rrev_str(), and thirdly you're modifying a string literal.
首先,您没有为反转的字符串分配足够的空间,其次,您将返回指向rrev_str()中的局部变量的指针,其次,您正在修改字符串文字。 You need to allocate space for revd_str on the heap:
您需要在堆上为revd_str分配空间:
char * rrev_str(char * str )
{
int i,r;
int str_len=strlen(str);
char *revd_str=malloc(str_len + 1);
memset(revd_str, 0, str_len + 1);
for (i = str_len-1, r=0; i >=0; i--,r++)
{
revd_str[r]= str[i];
}
return revd_str;
}
Problem: 问题:
You are accessing invalid memory address. 您正在访问无效的内存地址。
revd_str
is pointing to literal constant string of length 1 and you are accessing it beyond the length which is invalid. revd_str
指向长度为1的文字常量字符串,而您正在访问的字符串超出了无效的长度。
Solution: 解:
char * rrev_str(char * src, char *dest);
char * rrev_str(char * src, char *dest);
Reverse the given string 反转给定的字符串
char * rrev_str(char * str )
{
int start = 0;
int end = strlen(str) - 1;
char temp;
for (; start < end; start++ ,end--)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
}
return str;
}
int main(int argc, char* argv[])
{
char string [] ="smile";
//reverse "chars" in a C string and return it
char * rev_string = rrev_str(string);
printf("%s",rev_string);
}
Pass 2nd param as destination string 将第二个参数传递为目标字符串
char * rrev_str(char * src, char *dest)
{
int srcLength = strlen(src);
int destLength = strlen(dest);
int i;
// Invalid destination string
if (srcLength > destLength)
{
return NULL;
}
dest[srcLength] = '\0';
srcLength--;
for (i=0; srcLength >= 0;i++, srcLength--)
{
dest[i] = src[srcLength];
}
return dest;
}
int main(int argc, char* argv[])
{
char string [] ="smile";
char revString[20];
//reverse "chars" in a C string and return it
char * rev_string = rrev_str(string, revString);
printf("%s",rev_string);
}
What! 什么! you are doing..
你在做..
char *revd_str=""; // Creating String Literal which can't be modified because they are read only
char *revd_str[]=""; // Creating Char Array of Size Zero.
Either take reference of your string 请参考您的字符串
char *revd_str = strdup(str);
Or create dynamic char array 或创建动态char数组
char *revd_str = (char*) malloc (strlen(str)+1);
your program will run fine. 您的程序将正常运行。 logic is incorrect for reversing so modify it.
反向逻辑不正确,请对其进行修改。 A sample solution is given below
样品溶液如下
char * rrev_str(char * str )
{
char *revd_str=strdup(str);
int i; // no need for extra 'int r'
int str_len=strlen(str);
for (i = 0; i < str_len/2; i++)
{
char temp = revd_str[i];
revd_str[i]= revd_str[str_len - 1 -i];
revd_str[str_len - 1 -i] = temp;
}
return revd_str;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.