Thread: error in comparing pointer and intger

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    error in comparing pointer and intger

    delete
    Last edited by clag; 10-03-2009 at 05:32 PM. Reason: de

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You shouldn't be comparing it to null, you should be comparing it to '\0' ... it's not the same thing. Why don't you just use fputs?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    what should I return if I use fputs...will mine still be valid?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your function is a void fuction. You can't return anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    delete
    Last edited by clag; 10-03-2009 at 05:32 PM. Reason: delete

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    if I just use fputc, it compiles fine, but is the program erroneous?
    Last edited by clag; 10-03-2009 at 12:20 PM.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    man fputs:
    [code=cpp]int fputs(const char *restrict s, FILE *restrict stream);[/code]

    When you do this:
    Code:
    message[index]
    with a
    Code:
    const char *message
    you're passing a character, not a pointer to a character. That's what the compiler is complaining about.

    Any reason you're not just doing
    Code:
    fprintf(stderr, message);
    ???

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    cause Values of message are expected to be a NULL terminated character arrays.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just pass message, not one character from it.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    delete
    Last edited by clag; 10-03-2009 at 05:33 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you trying to return? I mean, you're passing along a message and a place to write it, but what are you trying to have it return? What do you want your return value to mean or indicate?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >but i feel like i'm returning it incorrectly..cuz message[index] would just be message[0] since it exited the loop.
    >should i just return message? but that's not long..
    Well, this doesn’t make any sense. You cant return long using char *. And why do you expect the index to be 0. Its declared within the function scope and to try to increment in the while loop which is still visible to all those usages in index within the function scope.

    Now tell us what you are trying to return from the function?

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    delete
    Last edited by clag; 10-03-2009 at 05:33 PM.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So in this example, your main neither wants nor expects you to return anything; so don't.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void writeline( const char *s, FILE *f )
    {
        if( s && f )
            fputs( s, f );
    }
    So just use fputs like we said a long time ago.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed