1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /* this is a "find" program: usage: find -options pattern -x shows the lines without the pattern instead of the one that actually contains it. -n print line numbers */ main(int argc, char *argv[]) { while(--argc > 0) printf((argc > 1) ? "%s " : "%s", *++argv); printf("\n"); return 0; } #endif #include <stdio.h> #include "mylib.c" main(int argc, char *argv[]) { char line[MAXLEN]; long lineno = 0; int c, exept = 0, number = 0, found = 0; while(--argc > 0 && (*++argv)[0] == '-') while(c = *++argv[0]) switch(c) { case 'x': exept = 1; break; case 'n': number = 1; break; default: printf("find: illegal option\n"); argc = 0; found = -1; break; } if(argc != 1) printf("Usage : find -x -n pattern\n"); else while(getline(line, MAXLEN) > 0) { lineno++; if((strstr(line, *argv) != NULL) != exept) { if(number) printf("%ld:", lineno); printf("%s", line); found++; } } return found; } |
If I use 0(line 49) I get no warning(and that is strange since the book use NULL), if I use NULL(that is what is used in the book, so it should be correct) the compiler says "warning C4047: '!=': 'int' differs in levels of indirection from 'void *'".
I see that NULL is defined as ((void *)0 in c and 0 in c++. But since I'm still Learning and maybe I miss some pieces of the puzzle. I don't get even this cast from a constant, what does it mean? I see that pointers normally get only addresses and 0 is the only exception from that. Hope somebody can unravel this in a simple way. I thought a null pointer can be compared to 0, and that is what the compiler seems to like, so why is NULL ((void *)0?
It was a surprise to find the actually NULL change from C to C++!
Thanks guys!