Home > Back-end >  Comparison of pointers that contain the same address?
Comparison of pointers that contain the same address?

Time:12-09

My function adds all elements of an array together and takes the "start" pointer and the "end" pointer(I know there are easier ways to get the sum). My problem is that my for-loop is skipped. But if I test the condition separately it works. Does that have anything to do with the order of execution of the for-loop? My example:

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.

CodePudding user response:

if i==0 then the condition is false, so the loop is not executed.. if you want to go through the array you should write &start[i] != end for the condition

  • Related