Comparing the contents of arrays in C -
does know why code printing "incorrect pin entered" after if
statement, instead of "correct pin entered"? when alter if
statement (input_pin != current_pin)
works.
#include <stdio.h> #define pin_size 4 main() { int input_pin[pin_size]; int current_pin[pin_size] = {1,2,3,4}; int i; for(i = 0; < pin_size; i++) { printf("enter pin %d: ", i+1); scanf("%d", &input_pin[i]); } if(input_pin == current_pin) { printf("\ncorrect pin entered.\n"); } else { printf("\nincorrect pin entered!\n"); } flushall(); getchar(); }
the if(input_pin == current_pin)
comparing 2 integer arrays. in c, arrays represented internally pointers. it's if comparing 2 int *
variables. because input_pin
, current_pin
different arrays, 2 pointers never compare equal.
to accomplish comparing you're trying make, need compare each element of each pin individually.
c arrays
No comments:
Post a Comment