【问题标题】:Comparison of pointers that contain the same address?比较包含相同地址的指针?
【发布时间】:2022-12-09 04:23:40
【问题描述】:

我的函数将数组的所有元素加在一起,并获取“开始”指针和“结束”指针(我知道有更简单的方法来求和)。我的问题是我的 for 循环被跳过了。但是,如果我单独测试条件,它就会起作用。这与for循环的执行顺序有什么关系吗? 我的例子:

int arr[]={3, 2, 1, 1}
int *start = &arr[0]
int *end = &arr[3]


printf("%d\n", (&start[0] == end)) //The result is 0(false)


printf("%d\n", (&start[3] == end)); // The result is 1(true)


for (int i = 0; (&start[i] == end); i++) // The for-loop dosen't get executed.

【问题讨论】:

    标签: pointers


    【解决方案1】:

    if i==0 那么条件为假,所以循环不执行.. 如果你想遍历数组,你应该为条件写&start[i] != end

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 2020-02-11
      • 2011-08-11
      相关资源
      最近更新 更多