c - Why is not these two return of bool equal? -
these 2 return statements not seem equal , wonder why?
bool isfound = false; return isfound;
returns true no matter value of isfound.
bool isfound = false; return isfound ? true: false;
returns value of isfound in case false.
i guess answer might isfound local variable , destroyed after function finished.
the return type of function const bool const doesn't seem make difference.
bool secventialsearch(int* arr, int size, int target, long* time){ struct timeval start, stop; bool isfound = false; long seconds, useconds; int* iter = arr; gettimeofday(&start, null); while (*iter != target && iter != &arr[size-1]) iter++; if (*iter == target ) isfound = true; gettimeofday(&stop, null); seconds = stop.tv_sec - start.tv_sec; useconds = stop.tv_usec - start.tv_usec; *time = ((seconds)*1000000 + useconds); return isfound; }
returns true in following code in main if target not in array. altering isfound ? true: false; makes work properly.
if (secventialsearch(testarr,testarrsize,5, &test)) printf("is true."); else printf("is false.");
i'm assuming variable declared boolean type (bool isfound
).
the expression isfound ? true : false;
redundant.
it evaluate true
if isfound
true, or false
if isfound
false.
it has exact same behavior writing isfound
. return isfound ? true : false;
equivalent return isfound;
the same applies code like
if (isfound) { return true; } else { return false; }
which same writing return isfound;
edit: said returning true
:
bool isfound = false; return isfound;
double check rest of code. alone sure return false
.
Comments
Post a Comment