通过与指针数组下循环 [英] C for loop through array with pointers

查看:113
本文介绍了通过与指针数组下循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C,但我在Java和Android体验。
我在for循环有问题。它永远不会结束,只是继续下去。

I'm new to C but I have experience in Java and Android. I have a problem in my for loop. It will never end and just go on and on.

char entered_string[50];
char *p_string = NULL;

gets( entered_string );

for( p_string = entered_string; p_string != '\0'; p_string++ ){
    //....
}

我知道,得到的是不安全,不建议去precated但根据我的规格我不得不使用它。我用指针要通过每个元素循环。

I know that gets is unsafe, not recommended and deprecated but according to my specs I have to use it. I want to loop through each element by using pointers.

推荐答案

您测试应该是 * p_string ='\\ 0';!

p_string 是一个指针,你的循环检查,如果指针!='\\ 0'。你有兴趣,如果该值!='\\ 0',并用让你不得不取消对它的引用了指针的值*

p_string is a pointer, and your loop is checking if the pointer is != '\0'. You're interested in if the value is != '\0', and to get the value out of a pointer you have to dereference it with *.

这篇关于通过与指针数组下循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆