Thread: Pointer question: *ptr with gets()

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    Pointer question: *ptr with gets()

    Hi everyone:

    I wonder why when i use gets() function in statement generates an error, but when used with a pointer: *ptr it works fine:

    " if (gets(input) != NULL) " -----generate an error
    " if (*(ptr=gets(input) !=NULL)" -----works fine

    what exactly does the pointer do here exactly??

    Hopfully all those experienced programmers can answer this question....

    ty.

  2. #2
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    Can you give us some more code to work with? Like a little example that does what you're talking about?
    Code:
    /* Little example */
    #include <stdio.h>
    
    int main(void)
    {
        char line[10];
    
        if (gets(line) != NULL)
            puts("Woohoo! It works!");
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    13
    yah..just exactly like wat u have......

  4. #4
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    Then I have no idea, it works fine for me. Try using fgets instead of gets and see how that works.
    Code:
    /* Little example */
    #include <stdio.h>
    
    int main(void)
    {
        char line[10];
    
        if (fgets(line, sizeof (line), stdin) != NULL)
            puts("Woohoo! It works!");
    
        return 0;
    }
    Or if that doesn't work, you can use scanf.
    Code:
    /* Little example */
    #include <stdio.h>
    
    int main(void)
    {
        char line[10];
    
        if (scanf("%9s", line) == 1)
        {
            int c;
    
            if ((c = getc(stdin)) != '\n' && c != EOF)
                ungetc(c, stdin);
    
            puts("Woohoo! It works!");
        }
    
        return 0;
    }
    Or write your own if you really need to.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @muk_luk
    >>when i use gets() function in statement generates an error
    Knowing what the error actually is might gives a bit more of a clue. Post some code too if you want help.

    If you want more information, try here too.

    [edit]
    >>" if (*(ptr=gets(input) !=NULL)" -----works fine
    This won't work fine, it doesn't have enough brackets, and therefore won't compile.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer

    >>" if (*(ptr=gets(input) !=NULL)" -----works fine
    This won't work fine, it doesn't have enough brackets, and therefore won't compile.
    Well actually, it depends. He does have it in quotes, so it's just a string literal.

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

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    9

    Post Re: Pointer question: *ptr with gets()

    Originally posted by muk_luk
    Hi everyone:

    I wonder why when i use gets() function in statement generates an error, but when used with a pointer: *ptr it works fine:


    ty.
    as i see , no one gave u the answer till now,
    the answer for ur question is the following,

    u should first determine the function prototype for gets
    here it is

    char *gets(char *s)
    that means that this function reads character string and puts this string in a string s {pointer]

    that is why u should use it with pointer

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Re: Pointer question: *ptr with gets()

    Originally posted by system_eyes
    as i see , no one gave u the answer till now,
    No, that has nothing to do with why he didn't get an answer. He didn't get an answer he liked because he failed to provide enough code. What is 'input' in his code? Can you be sure? No, you can't. He never says what 'input' is.

    Furthermore, the provided code that he says works wouldn't even compile, because he doesn't have enough ) marks.

    And finally, he never says what error he gets. Simply stated, he says "generates an error". Well that's all fine and dandy, but it does absolutely nothing for us when we're trying to tell him what he's doing wrong.

    In short, if he had avoided gets all together, he wouldn't be having that problem.

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

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    13
    thank you for all ur help.....


    p.s: Quzah= stubbornnnnnn!

  10. #10
    Banned
    Join Date
    May 2003
    Posts
    124
    >> *(ptr=gets(input)

    Oh God! Are you sure about the language you are writing in?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM