一个简单的程序,但是有一个问题十分不解
程序代码:#include<stdio.h>
#include<string.h>
#include <stdlib.h>
void dy(char*a); //应申明函数
int main ( )
{
char a[50];
printf("输入一串数字:");
fgets(a,sizeof a,stdin); //不能用scanf读取,因为它一碰到空格就结束字符串类型的读入
a[strlen(a)-1] = '\0';
dy(a);
printf("得出结果:%s",a);
return 0;
}
//在这里改了很多,具体没有说明,自己先看看吧
void dy(char a[])
{
int i=0;
char *temp = (char*)malloc(sizeof a);
strcpy(temp,a);
while((strlen(temp)-2-i) >= 0)
{
printf("str == %d\n",strlen(temp)-2-i);
a[i] = temp[strlen(temp)-2-i];
i++;
printf("%d",i);
}
a[i] = '\0';
printf("%d\n",strlen(a));
return;
}
出现了比较严重的问题,检查这里发现:
程序代码: while((strlen(temp)-2-i) >= 0)
{
printf("str == %d\n",strlen(temp)-2-i);
a[i] = temp[strlen(temp)-2-i];
i++;
printf("%d",i);
}
当(strlen(temp)-2-i)的值为负1时
并没有跳出while循环,怎么会这样呢?









