August 13th, 2003, 06:53 PM
Bug in C code
Print statement never executed
progam_name = argv;
parameter1 = argv;
if (argc < 2)
if (argc == 2 && parameter1 == "long")
PROBLEM -->> printf ("parameter1 = %s\n", parameter1);
August 13th, 2003, 07:04 PM
parameter1 is a pointer to a location in memory, this location is the beginning of the values 'l', 'o', 'n', 'g', '\0' you cannot compare all of these values in one go using ==.
You need to compare each value individually, for example:
char *p1 = argv; /* this should be check first... */
Look into the function "strcmp", from string.h.
if(strlen(argv) > 3)
if(*p1 == 'l' && *(p1+1) == 'o' && *(p1+2) == 'n' && *(p1+3) == 'g' && *(p1+4) == '\0')
printf("parameter1 == %s\n", p1);
You would use it follows:
Please remember error checking should be used to check that these arguments actually exist before attempting to access the memory of them.
res = strcmp(argv, "long");
if(res == 0)
printf("argv == %s\n", argv);
Last edited by xtor; August 13th, 2003 at 07:09 PM.
August 13th, 2003, 07:09 PM
Thank you for your help. This is one of my most common mistake. You must use the strncmp, not direct comparison.
Too many languages, too many syntax, and different features.