简体   繁体   English

为什么处理完指向字符串的指针后为空输出?

[英]Why empty output after processing pointer to string?

I have written the code that converts a string, entered by the user to lowercase, in two ways. 我已经编写了两种将用户输入的字符串转换为小写字母的代码。

//method 1:
void xstrlwr(char*p)
{
    while(*p!='\0')
    {
        if(*p>=65&&*p<=90)
        {
            *p=*p+32;
        }
        p++;
    }
    printf("%s",p);
}

and

//method 2:
void xstrlwr(char*p)
{
    int l,i;
    l=strlen(p);
    for(i=0;i<=l;i++)
    {
        if(p[i]>=65&&p[i]<=90)
        {
            p[i]=p[i]+32;
        }
    }
    printf("\n%s",p);
}

here is the expected output 这是预期的输出

Input : WElcome TO sTack OverFLow
Expected output: welcome to stack overflow

the code 1 is not running correctly (printing the string has empty output), 代码1运行不正常(打印字符串输出为空),
whereas the code 2 is running perfectly (it outputs the lowercase string). 而代码2运行完美(它输出小写字符串)。
So my question is why the first one has empty output although both codes are logically identical, just having different notations? 所以我的问题是,尽管两个代码在逻辑上都是相同的,只是符号不同,但为什么第一个输出却为空?

Your printf in the first snippet is not printing anything since the pointer points to the NUL terminator. 您的第一段代码中的printf没有打印任何内容,因为指针指向NUL终止符。 (Note the use of p++ in that snippet.) (请注意在该代码段中使用了p++ 。)

In the second snippet you don't change the parameter passed to the function, so p still points to the start of the string. 在第二个代码段中,您无需更改传递给函数的参数,因此p仍指向字符串的开头。

The "guts" of both functions are identical although I prefer the first one. 尽管我更喜欢第一个功能,但这两个功能的“胆量”是相同的。 The moral of the story here is to print the results at the function calling site, not in the function itself. 这里故事的寓意是在函数调用站点而不是在函数本身中打印结果。

Finally note that either algorithm only works for platforms using ASCII (or a close cousin) for character encoding. 最后请注意,这两种算法仅适用于使用ASCII(或近亲)进行字符编码的平台。 For this reason, the C standard library provides tolower to do this job. 因此,C标准库提供了tolower来完成此工作。

both codes are logically same just having different notations? 这两个代码在逻辑上是相同的,只是符号不同?

That is not true. 那是不对的。 In method2 the pointer p never changed. 在方法2中,指针p从未更改。 At the end of the loop it still points to the start of the string. 在循环末尾,它仍然指向字符串的开头。 And in method1 after the end of the loop, p points to \\0 , the null terminator of the input string (because of p++ ). 在循环结束后的method1中, p指向\\0 ,即输入字符串的空终止符(由于p++ )。 You can create a copy of p to fix it. 您可以创建p的副本来修复它。

void xstrlwr(char*p)
{
    char *q = p;                // A copy
    while(*p!='\0')
    {
        if(*p>=65&&*p<=90)
        {
            *p=*p+32;
        }
        p++;
    }
    printf("%s",q);           // q not p
}

Maybe this is just an exercise in playing with ASCII, but if not you should really be using tolower() instead of bit-banging. 也许这只是使用ASCII的一种练习,但是如果没有,您应该真正使用tolower()而不是位敲打。 It's more portable. 更便携。

EDIT: This would have been better as a comment, since it's more of an aside/observation than an answer to the question. 编辑:这本来可以作为评论更好,因为它更多地是在旁听/观察而不是对问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM