在我理解问题时的C指针错误

huangapple go评论53阅读模式
英文:

C pointer mistakes in my understanding of the problem

问题

执行后的输出结果为:

#include<stdio.h>
int main()
{  char str[]="xyz",*ps=str;
   while(*ps) 
       ps++;
   for(ps--;ps-str>=0;ps--)
       puts(ps);
   return 0;
}

正确答案是:
z
yz
xyz

我的逻辑是:
while(*ps) ps++;之后,*ps表示数组的最后一个元素,即" "
当执行代码for(ps--;ps-str>=0;ps--)puts(ps);时,*ps每次向前移动两个位置。
所以我认为答案应该是
z
xyz

英文:

The output result after execution is______<br/>

#include&lt;stdio.h&gt;
int main()
{  char str[]=&quot;xyz&quot;,*ps=str;
   while(*ps) 
       ps++;
   for(ps--;ps-str&gt;=0;ps--)
       puts(ps);
   return 0;
}

The right answer is <br/>
z<br/>
yz<br/>
xyz<br/>

My logic is<br/>
After while(*ps) ps++; *psrepresents &quot; &quot; the last element of the array<br/>
When execute codefor(ps--;ps-str&gt;=0;ps--)puts(ps);,*ps go back two positions everytime.
So I think t should be <br/>
z<br/>
xyz<br/>

答案1

得分: 2

可能更接近你想要的:

int main()
{  
   char str[]="xyz";
   size_t length = sizeof(str)-1;
   // size_t length = strlen(str); // 更安全的版本。

   for (size_t i = 0; i < length; i++)
   {
       char* ps = str+length-i-1;
       puts(ps);
   }
}
英文:

Probably closer to what you want:

int main()
{  
   char str[]=&quot;xyz&quot;;
   size_t length = sizeof(str)-1;
   // size_t length = strlen(str); // safer version of above.
 
   for (size_t i = 0; i &lt; length; i++)
   {
       char* ps = str+length-i-1;
       puts(ps);
   }
}

答案2

得分: 1

No, *ps is then &#39;\0&#39;.

No, it does not. It starts by going back one step, then in each iteration it only goes back one step. It's the same as doing this:

{
    ps--;                 // for(ps--; ...; ...)
    while(ps-str &gt;= 0) {  // for(...; ps-str &gt;= 0; ...)
        puts(ps);
        ps--;             // for(...; ...; ps--)
    }
}

ps-str&gt;=0 is not a good test though. If you step ps "behind" the start of str the comparison is invalid.

A safe version could simply be:

#include &lt;stdio.h&gt;

int main() {
    char str[] = &quot;xyz&quot;, *ps = str;

    while (*ps) ps++;

    while (ps != str) {
        --ps;
        puts(ps);
    }
}
英文:

> "After while(*ps) ps++; *ps represents &quot; &quot;"

No, *ps is then &#39;\0&#39;.

> When execute code for(ps--;ps-str&gt;=0;ps--)puts(ps);, *ps go back two positions everytime

No, it does not. It starts by going back one step, then in each iteration it only goes back one step. It's the same as doing this:

{
    ps--;                 // for(ps--; ...; ...)
    while(ps-str &gt;= 0) {  // for(...; ps-str &gt;= 0; ...)
        puts(ps);
        ps--;             // for(...; ...; ps--)
    }
}

ps-str&gt;=0 is not a good test though. If you step ps "behind" the start of str the comparison is invalid.

A safe version could simply be:

#include &lt;stdio.h&gt;

int main() {
    char str[] = &quot;xyz&quot;, *ps = str;
    
    while (*ps) ps++;
    
    while (ps != str) {
        --ps;
        puts(ps);
    }
}

huangapple
  • 本文由 发表于 2023年2月6日 14:42:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定