Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparison of pointers that contain the same address?

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.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading