Thread: Question !!!! Warning Message!!!!

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    Question Question !!!! Warning Message!!!!

    Hi all,

    I'm doing a program which is input a text file, and match every punctuation in that text file.

    But when i compiled the program, it gave me a warning message which is "passing argument 2 of 'regexec' makes pointer from integer without a cast."

    here is my code.

    Code:
    void process(FILE *fp, int width, int punctuation)
    {
    
        char single_c;
    
        int rc, i, pos = 0;
    
        regex_t * myregex = calloc(1, sizeof(regex_t));
    
        rc = regcomp( myregex, "[^0-9a-zA-Z ]+", REG_EXTENDED | REG_NOSUB ); //regular expression.
                                                                             //case: punctuation
    
        while ( (single_c = getc(fp) ) != EOF)
        {
            if( 0 == regexec(myregex,(char )single_c, 0 , 0 , 0 ) )
            {
                printf("String %c matches.\n", single_c );
            }
            else
            {
                printf("String %c matches.\n", single_c );
            }
        }
    }
    Can someone tell me how to fix this problem?
    Last edited by her4men; 02-27-2010 at 07:43 PM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The second argument to regexec() is a char*, not a char. You must pass a string.

    You should also avoid casting. First, casting a char to a char is meaningless. But generally speaking--especially if you're new to C--a cast is the wrong thing to do. They are, of course, useful in some cases, but nowhere near as often as they tend to be used.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    5
    oh~!! ok!

    I got it. Thanks.

    By the way, I have found lots example, and most of them use pointer to do converting, for example:

    char a;
    char *b;

    b=&a

    I just wondering that is C uses pointer a lot??
    Is there other way to convert single char to string?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,275
    Quote Originally Posted by her4men
    I just wondering that is C uses pointer a lot??
    "A lot" is relative, but yes.

    Quote Originally Posted by her4men
    Is there other way to convert single char to string?
    That does not convert a single char to a string. It simply assigns the address of a char to a pointer to char. No strings are involved. If you want to convert a single char to a string, create an array of two chars, and assign the given char to the first element of the array, and a null character to the second element of the array. The array would then contain a string.
    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

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    another error

    There's a strange thing appeared in my program
    Code:
    void process(FILE *fp, int width, int punctuation)
    {
    
        char single_c,
             *temp_char,
             whole_String[width],
             temp_String[width];
    
        int rc, i, pos = 0;
    
        regex_t * myregex = calloc( 1, sizeof(regex_t) );
    
        rc = regcomp( myregex, "[^a-zA-Z0-9 ]+", REG_EXTENDED | REG_NOSUB );
        printf("RC from regcomp() = %d\n", rc); //regular expression.
                                                                   //case: punctuation
    
        while ( ( single_c = getc(fp) ) != EOF && width != pos)
        {
            temp_char = &single_c; //string point to char.
            if( 0 == regexec(myregex, temp_char, 0 , 0 , 0 ) )
            {
    
                printf("%c\n", single_c );
            }
            else
            {
                printf("abc");
            }
    
            pos++;
        }
    
        free(myregex);
    }
    In the if statement , my program never go into else part.
    Suppose when I give ":" to stdin, then this should print out "abc".

    Can anyone tell me what my problem is?

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
      temp_char = &single_c; //string point to char.
    Assigning a pointer to char (char *) to a char type does not result in a "string" as should be passed to regexec.

    But I'm curious is why are you using a regexec against a SINGLE character? Shouldn't you be testing against the entire text of the file, ie a long string (which you would have to allocate enough memory to fit the text in)

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,533
    > temp_char = &single_c; //string point to char.
    And where is the \0 to mark the end of the string in this ?

    Simply declaring a bunch of variables with the right type isn't enough.

    As nonpuz says, testing a single char with a regex is pointless.

    It seems to me you need
    Code:
    char buff[BUFSIZ]
    while ( fgets( buff, sizeof buff, fp ) != NULL ) {
      if( 0 == regexec(myregex, buff, 0 , 0 , 0 ) )
    
    }

    Oh, and I also find operand reversal to be annoying and counter-productive.
    Question 17.4

    Your modern compiler will almost certainly warn you (and is certainly capable of warning you) if you write if ( var = 0 )

    You would get an error anyway if you did
    if ( regexec(myregex, buff, 0 , 0 , 0 ) = 0 )
    because the function result isn't an lvalue.

    Further, if you find yourself with
    if ( var1 = var2 )
    then no amount of rearranging the deckchairs will save your program from sinking.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  2. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. Warning message truncation 'double' to 'float'
    By Tojam in forum C Programming
    Replies: 4
    Last Post: 05-01-2002, 02:21 AM
  5. warning message
    By potbelle in forum C Programming
    Replies: 11
    Last Post: 04-03-2002, 10:20 PM