help with this tutorial program: invalid operands to binary( - )

This is a discussion on help with this tutorial program: invalid operands to binary( - ) within the C Programming forums, part of the General Programming Boards category; Hello, I am just going over tutorial on line and working on this character handling program from on line tutorial. ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    help with this tutorial program: invalid operands to binary( - )

    Hello, I am just going over tutorial on line and working on this character handling program from on line tutorial.

    First of all, I just copy the program but it doesn't compile.
    It complains that (int)cp-in_line is illegal

    cha_handle.c:21: error: invalid operands to binary - (have ‚int‚ and ‚char *‚)


    Also, I don't understand why in_line(begining of array) need to be subtracted from current position in array(denoted by cp)...

    Any help would be appreciated.

    thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LINELNG 100 /* max length of input line */
    
    main() {
       char in_line[LINELNG];
       char *cp;
       int c;
    
       cp = in_line;
       while (  ( c=getc(stdin) ) != EOF   ) {
           if (  cp == &in_line[LINELNG-1] || c == '\n' ) {
                *cp = 0;
                if ( strcmp( in_line, "stop" ) == 0 )
                      exit( EXIT_SUCCESS );
                else
                      /* printf("line was %d characters long\n", (int)cp-in_line); */
                      printf("line was %d characters long\n",
                            (int)cp-in_line);
                cp = in_line;
           }
           else
               *cp++ = c;
        }
        exit(EXIT_SUCCESS);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Maybe that's supposed to be (int)(cp-in_line) -- do the pointer arithmetic to find the number of characters moved over, then cast to integer.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    that didn't work..

    anything else?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by convenientstore View Post
    that didn't work..

    anything else?
    It certainly should work. You may want to post your current line that has the problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Hi, here it is.

    Code:
    root@userA-laptop:/sc/c# cat cha_handle.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LINELNG 100 /* max length of input line */
    
    main() {
       char in_line[LINELNG];
       char *cp;
       int c;
    
       cp = in_line;
       while (  ( c=getc(stdin) ) != EOF   ) {
           if (  cp == &in_line[LINELNG-1] || c == '\n' ) {
                *cp = 0;
                if ( strcmp( in_line, "stop" ) == 0 )
                      exit( EXIT_SUCCESS );
                else
                      /* printf("line was %d characters long\n", (int)cp-in_line); */
                      printf("line was %d characters long\n",
                            (int)(cp-in_line);
                cp = in_line;
           }
           else
               *cp++ = c;
        }
        exit(EXIT_SUCCESS);
    }
    root@userA-laptop:/sc/c# gcc -o cha_handle cha_handle.c
    cha_handle.c: In function ‚€˜main‚€™:
    cha_handle.c:21: error: expected ‚€˜)‚€™ before ‚€˜;‚€™ token
    cha_handle.c:23: error: expected ‚€˜;‚€™ before ‚€˜}‚€™ token
    root@userA-laptop:/sc/c#
    
    line 21 being
         21                         (int)(cp-in_line);
         22             cp = in_line;
         23        }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You don't have the same number of ( and ). Check carefully that they match up [many editors can show which starting one matches the end one or vice versa by flashing/colouring the opposite one].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    my gosh, yes thank you so much.

    Code:
    (int)(cp-in_line));
    I just have one more question..

    why was there a need to subtract in_line from cp ?

    in_line is begining of array which is basically 0.... and cp should have the length of input.. or am i thinking of this as none c...
    is the program really subtracting address?

    so in_line == address of 1000(begining of array)
    cp == address of 1009(at the end of input)

    Am i looking at this correctly?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The 1000 / 1009 interpretation is correct.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    thank you!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. misc.c:3210: invalid operands to binary +
    By Unregistered in forum Linux Programming
    Replies: 2
    Last Post: 07-19-2002, 07:00 AM
  5. invalid operands
    By simhap in forum C++ Programming
    Replies: 5
    Last Post: 10-08-2001, 10:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21