Thread: Set X to 5

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    Set X to 5

    so i'm reading an exercise on structures in the C k&R book.

    it says to modify getword routine in order to take in to consideration

    underscores, comments etc.

    i'm trying to start small (i.e check for begining of comment and set X to 5 if found) before writing the actual code but it seems hard apparently. heres the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    #define MAXWORD 100
    
    #define NKEYS (sizeof keytab/sizeof(keytab[0]))
    
    #define CINDEX 12
    
    
    #define BUFSIZE 100
    
    char buf[BUFSIZE]; /* buffer for ungetch */
    int bufp = 0; /* next free position in buf */
    int getch(void) /* get a (possibly pushed-back) character */
    {
        return (bufp > 0) ? buf[--bufp] : getchar();
    }
    void ungetch(int c) /* push character back on input */
    {
        if (bufp >= BUFSIZE)
            printf("ungetch: too many characters\n");
        else
            buf[bufp++] = c;
    }
    
    struct key{
        char *word;
        int count;
    } keytab[] = {
         {"auto", 0},
         {"break", 0},
         {"case", 0},
         {"char", 0},
         {"const", 0},
         {"continue", 0},
         {"default", 0},
         {"for", 0},
         {"unsigned", 0},
         {"signed", 0},
         {"volatile", 0},
         {"void", 0},
         {"comments", 0},
         {"while", 0}
    };
    int getword(char *, int);
    int binsearch(char *, struct key *, int);
    int main(){
        int n;
        char word[MAXWORD];
    
        while(getword(word, MAXWORD) != EOF)
            if( isalpha(word[0]))
                if ( (n = binsearch(word, keytab, NKEYS)) >= 0)
                    keytab[n].count++;
        for( n = 0; n < NKEYS; n++ )
            printf("%4d  %s\n", keytab[n].count, keytab[n].word);
    
        return 0;
    }
    
    int getword(char *word, int lim){
        int c, getch(void);
        void ungetch(int c);
    
        char *w = word;
        while(isspace(c = getch()))
            ;
        if(c != EOF || c == '/'){
            if(c == '/'){
                c = getch();
                if( c == '*'){
                     int x = 5;
                }
            }
            *w++ = c;
        }
        if (!isalpha(c)){
            *w = c;
            return c;
        }
    
        for(; --lim > 0; w++)
        if(!isalnum(*w = getch())){
            ungetch(*w);
            break;
        }
        *w = '\0';
        return w[0];
    }
    
    int binsearch(char *word, struct key keytab[], int n){
        int cond;
        int low, mid, high;
    
        low = 0;
        high = n - 1;
        while(low <= high){
            mid = (low+high)/2;
    
            if( (cond = strcmp(word, keytab[mid].word)) < 0)
                high = mid - 1;
            else if ( cond > 0)
                low = mid + 1;
            else
                return mid;
        }
        return -1;
    
    }
    when i step through the code in a debugger, i first input '/' then '*' without quotes and i assume this code

    Code:
    if(c == '/'){
                c = getch();
                if( c == '*'){
                     int x = 5;
                }
            }
    should handle it, how ever, execution goes thus;
    Code:
    ]if(c == '/'){ // evaluates as TRUE so rest code executes
                c = getch(); // debugger just skips this, i don't get to input '*'
                if( c == '*'){ // thus, this is is skipped
                     int x = 5;
                }
            }
    what i'm i doing wrong please?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It seems fine.
    Code:
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b 71
    Breakpoint 1 at 0x40088c: file foo.c, line 71.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    /* foo */
    
    Breakpoint 1, getword (word=0x7fffffffdda0 "", lim=100) at foo.c:71
    71	    if(c != EOF || c == '/'){
    (gdb) p/c c
    $1 = 47 '/'
    (gdb) n
    72	        if(c == '/'){
    (gdb) 
    73	            c = getch();
    (gdb) 
    74	            if( c == '*'){
    (gdb) p/c c
    $2 = 42 '*'
    (gdb) n
    75	                 int x = 5;
    The problem is, x is locally declared within that block, and you don't use x for anything. So it's all too easy for the compiler to remove it as an unused statement.

    Eg.
    Code:
    $ gcc -Wall -g foo.c
    foo.c: In function ‘getword’:
    foo.c:75:22: warning: unused variable ‘x’ [-Wunused-variable]
                      int x = 5;
                          ^
    Try with something a little more obvious, say
    Code:
                c = getch();
                if( c == '*'){
                     fprintf(stderr,"DEBUG: comment start\n");
                }
    Code:
    $ gcc -Wall -g foo.c
    $ ./a.out 
    auto
    auto
    /* comment */
    DEBUG: comment start
       2  auto
       0  break
       0  case
       0  char
       0  const
       0  continue
       0  default
       0  for
       0  unsigned
       0  signed
       0  volatile
       0  void
       0  comments
       0  while
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    31
    thanks very much, that has been resolved but now i don't know why ret which is to store the return value of getword in order to decide whether its a comment or normal keyword is always returning value 1.

    please help and sorry for my noobish questions @salem

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    #define MAXWORD 100
    
    #define NKEYS (sizeof keytab/sizeof(keytab[0]))
    
    #define CINDEX 12
    
    
    #define BUFSIZE 100
    
    char buf[BUFSIZE]; /* buffer for ungetch */
    int bufp = 0; /* next free position in buf */
    int getch(void) /* get a (possibly pushed-back) character */
    {
        return (bufp > 0) ? buf[--bufp] : getchar();
    }
    void ungetch(int c) /* push character back on input */
    {
        if (bufp >= BUFSIZE)
            printf("ungetch: too many characters\n");
        else
            buf[bufp++] = c;
    }
    
    struct key{
        char *word;
        int count;
    } keytab[] = {
         {"auto", 0},
         {"break", 0},
         {"case", 0},
         {"char", 0},
         {"const", 0},
         {"continue", 0},
         {"default", 0},
         {"for", 0},
         {"unsigned", 0},
         {"signed", 0},
         {"volatile", 0},
         {"void", 0},
         {"comments", 0},
         {"while", 0}
    };
    int getword(char *, int);
    int binsearch(char *, struct key *, int);
    int main(){
        int n;
        char word[MAXWORD];
        int ret;
        while(ret = (getword(word, MAXWORD) != EOF)){
            if (ret = CINDEX)
                keytab[CINDEX].count++;
            else {
            if( isalpha(word[0]))
                if ( (n = binsearch(word, keytab, NKEYS)) >= 0)
                    keytab[n].count++;
            }
        }
        for( n = 0; n < NKEYS; n++ )
            printf("%4d  %s\n", keytab[n].count, keytab[n].word);
    
        return 0;
    }
    
    int getword(char *word, int lim){
        int c, getch(void);
        void ungetch(int c);
    
        char *w = word;
        while(isspace(c = getch()))
            ;
        if(c != EOF || c == '/'){
            if(c == '/'){
                c = getch();
                if( c == '*'){
                    lim -= 2;
                    for(; (c = getch()) != '*' && lim > 2; lim--)
                        ;
                    if(c == '*'){
                        c = getch();
                        if(c == '/')
                            return CINDEX;
                    }
    
                }
            }
            else
                *w++ = c;
        }
        if (!isalpha(c)){
            *w = c;
            return c;
        }
    
        for(; --lim > 0; w++)
        if(!isalnum(*w = getch())){
            ungetch(*w);
            break;
        }
        *w = '\0';
        return w[0];
    }
    
    int binsearch(char *word, struct key keytab[], int n){
        int cond;
        int low, mid, high;
    
        low = 0;
        high = n - 1;
        while(low <= high){
            mid = (low+high)/2;
    
            if( (cond = strcmp(word, keytab[mid].word)) < 0)
                high = mid - 1;
            else if ( cond > 0)
                low = mid + 1;
            else
                return mid;
        }
        return -1;
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You absolutely must start using -Wall in your compilations to filter out all the stupid mistakes like using = when you mean ==
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:54:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         while(ret = (getword(word, MAXWORD) != EOF)){
         ^
    foo.c:55:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
             if (ret = CINDEX)
             ^
    Also, instead of returning some magic number, why don't you just store the word "comments" in the word parameter, then main works just the same as before without any need for special cases, or forgetting to change CINDEX when you add more words to your keytab.

    In addition, your keytab list MUST be sorted into alphabetic order for binsearch to work properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    31
    Quote Originally Posted by Salem View Post
    You absolutely must start using -Wall in your compilations to filter out all the stupid mistakes like using = when you mean ==
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:54:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         while(ret = (getword(word, MAXWORD) != EOF)){
         ^
    foo.c:55:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
             if (ret = CINDEX)
             ^
    Also, instead of returning some magic number, why don't you just store the word "comments" in the word parameter, then main works just the same as before without any need for special cases, or forgetting to change CINDEX when you add more words to your keytab.

    In addition, your keytab list MUST be sorted into alphabetic order for binsearch to work properly.

    i use codeblocks, how can i compile with -Wall flag?

    also, i believe comments are different from the other keywords in that they can be anything or any length as long as they fall
    within /* and */ but the others are fixed keywords so no way for strcmp to compare - this ofcourse is my own level of understanding. Please shed more light sir.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I don't mean return the content of the comment, I mean return the literal string "comments".

    Like
    strcpy(word,"comments");

    Also, this is how you enable -Wall
    Project->Build Options...
    Set X to 5-cb-wall-jpg
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should also check the -Wextra and -pedantic boxes and select one of the more modern C/C++ standards (C11/C99) as well.

    Jim

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    31
    Quote Originally Posted by Salem View Post
    I don't mean return the content of the comment, I mean return the literal string "comments".

    Like
    strcpy(word,"comments");

    Also, this is how you enable -Wall
    Project->Build Options...
    Set X to 5-cb-wall-jpg
    applied strcpy() as suggested, code is much neater and works too, thanks salem. :

    i have another problem tho, i was trying to implement binsearch using pointers but before changing the actual function i am playing with normal pointer arithmetic. see this
    Code:
    #include <stdio.h>
    
    
    int main(){
    
    int x[] = {1,2,3};
    int *y = x+2;
    int *z = x;
    int *mid;
    
    mid = (z+y)/2;
    
     printf("%d \t\t %p \n", *mid, mid);
    
    }
    when i compile i get error;

    C:\Users\xx\Documents\tc.c|11|error: invalid operands to binary + (have 'int *' and 'int *')|
    whats wrong? in k&R they used

    Code:
    
    
    struct key *low = &tab[0];
    struct key *high = &tab[n];
    struct key *mid;
    while (low < high) {
    mid = low + (high-low) / 2;
    
    
    and it works

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can subtract pointers (i.e., to obtain the difference in positions between the elements that the pointers point to), but you cannot add them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2017
    Posts
    31
    Quote Originally Posted by laserlight View Post
    You can subtract pointers (i.e., to obtain the difference in positions between the elements that the pointers point to), but you cannot add them.
    im confused right now, isn't both *low' and *high' pointers in the context used in the k & R?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thagulf2017
    im confused right now, isn't both *low' and *high' pointers in the context used in the k & R?
    Yes, but let's look at the statement from K&R:
    Code:
    mid = low + (high-low) / 2;
    Let's replace them with the types:
    Code:
    int* = int* + (int* - int*) / int;
    Now, let's resolve the expression on the right hand of the assignment, considering only the types:
    Code:
    int* = int* + (int) / int;
    int* = int* + int;
    int* = int*;
    I simplified by using int instead of ptrdiff_t. As you can see, at no point are pointers added together. Now let's contrast with yours:
    Code:
    mid = (z+y)/2;
    considering the types only:
    Code:
    int* = (int* + int*) / int;
    here we can immediately see that the expression does not make sense: what does it mean to add an int* to an int*? Therefore, it is an error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2017
    Posts
    31
    i think i understand this now (*high - *low) returns int not a pointer so it makes sense now, thanks salem

Popular pages Recent additions subscribe to a feed

Tags for this Thread