c - What is wrong in the if statement which checks a value from shared memory? -


in code value of flag determined value in shared memory. when print says value 1, because prints '1'. why program not proceed code in 'if (flag == "1")'?

char *      shm_addr = (char *) map_failed; char *      shm_name = "print"; int         size = -1; int         rtnval; char *      flag; int         numbertoprint = 0;  size = 32;  while (shm_addr == (char *) map_failed) {              shm_addr = my_shm_open (shm_name); }  sscanf (shm_addr, "%s", flag);  printf ("\ndata (@ %#x): '%s'\n", (unsigned int)(intptr_t) shm_addr, flag);  while (numbertoprint != 6) {     if (flag == "1")     {         numbertoprint += 2;         printf("%i ", numbertoprint + 2);         flag = "0";         sscanf (flag, "%s", shm_addr);     }                 } 

you code wrong in several places (you should compile warning activated): first, flag string not have space allocated char *flag should changed char flag[123 /* correct size */]. if don't that, first sscanf wrong (you have unexpected behaviour)

then if (flag == "1") compare 2 pointers (the 1 of flag 1 points "1", not want.

you want write if (strcmp(flag, "1") == 0) of if (*flag == '1')


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -