문자열을 뒤집어 봅니다.버스 오류 문제 해결
가#include<stdio.h>
#include<string.h>
int main(){
char *c="I am a good boy";
printf("\n The input string is : %s\n",c);
printf("\n The length of the string is : %d\n",strlen(c));
int i,j;
char temp;
int len=strlen(c);
for(i=0,j=len-1;i<=j;i++,j--)
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
//printf("%c\t%c\n",*(c+i),*(c+(len-i-1)));
}
printf("\n reversed string is : %s\n\n",c);
}
코드가 Bus error : 10
출력 :
I이 시도하는 코드이다.
그러나 나는 같은 코드를 다시 작성하는 경우 : 그것은 완벽하게 잘 작동
int main(void)
{
char *str;
str="I am a good boy";
int i,j;
char temp;
int len=strlen(str);
char *ptr=NULL;
ptr=malloc(sizeof(char)*(len));
ptr=strcpy(ptr,str);
for (i=0, j=len-1; i<=j; i++, j--)
{
temp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=temp;
}
printf("The reverse of the string is : %s\n",ptr);
}
.
첫 번째 코드가 버스 오류 또는 분할 오류를 발생시키는 이유는 무엇입니까?
코드를 다시 포맷하십시오. –
가능한 많은 서식을 시도했지만 정말 정렬되지 않은 방법을 알고 있습니다. – sethu
4 개의 공백으로 모두 들여 쓰기하여 코드를 포맷하십시오. – SLaks