如何检查指针是否在我的数组末尾?
这是我的代码。如果输入d
,指针在末尾,什么都不做;如果不是,向右移动。
if(d == 'd')
{
if (*p != a[6]) //a[6] is the array length // this is where i need help
{
++*p;
num_mov++;
print_status(a, *p, num_mov);
}
else
print_status(a, *p, num_mov);
}
最佳答案
使用条件
if ( p != a + 6)
考虑到这个声明
++*p;
不增加指针本身。它增加了指针指向的对象。
如果你想增加指针本身你应该写
++p;
同时检查函数 print_status
的第二个参数是否声明为指针。如果是这样,那么你必须写
print_status(a, p, num_mov);
插入
print_status(a, *p, num_mov);
关于c - 使指针指向数组的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33986388/