Pointer (adress) question.

This is a discussion on Pointer (adress) question. within the C Programming forums, part of the General Programming Boards category; Allright then. So if I understand correctly the following should be done: Code: char input [25]; char iPtr = input; ...

  1. #16
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Allright then. So if I understand correctly the following should be done:
    Code:
    char input [25];
    char iPtr = input;
    fgets(iptr, 25, stdin);
    To get one line from the user?

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Yes. The first argument specifies a buffer to use, like you do.
    The second argument tells the maximum amount of characters fgets can read before stopping.
    And the third indicates where it should read from - to read from the user, stdin is typically used.

    25 characters max be a little too little. Perhaps try extending it to 100 or more. And there you go.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Note that fgets() leaves the terminating newline (if any) in place, while gets() does not -- so if you want a drop-in replacement for gets() (with a length parameter, of course), consider something like this.
    Code:
    #include <stdio.h>  /* for fgets() */
    #include <string.h>  /* for strchr() */
    
    char *get_string(char *buffer, size_t len) {
        char *p;
        
        /* get a line of input, returning NULL on failure */
        if(!fgets(buffer, len, stdin)) return NULL;
        
        p = strchr(buffer, '\n');  /* search for a newline */
        if(p) *p = 0;  /* if there was a newline, terminate the string there, erasing it */
    
        return buffer;  /* just for compatibility with gets() and fgets() */
    }
    Note that I've added error checking and a return value, which you could leave out if you wanted to. Also, I've used size_t, which is the preferred type for referencing string indicies. (You can use int if it makes you feel better. ) A simpler version:
    Code:
    void get_string(char *buffer, int len) {
        fgets(buffer, len, stdin);
        p = strchr(buffer, '\n');
        if(p != NULL) *p = '\0';
    }
    Also see: http://cpwiki.sourceforge.net/Gets
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    25 characters max be a little too little. Perhaps try extending it to 100 or more. And there you go.
    How big the buffer should be is quite dependent on what you are asking for, right? If you expect the user to answer yes or no, you could get away with 4. If you are asking for a first name, you probably need about 20 to cover those lengthy double-names that are used in some places. For a full name (first + last), you are probably ok with 30-40 chars.

    What does make sense, however, is to write a function like dwks's, but also add a "clear the input buffer if too much input". There is of course nothing stopping a person from typing in "ajkhkfhkasdhfkasdhksdh" as the answer to "Yes or No" type question, and then you end up with "rubbish" in the input buffer. It's not really a problem that "ajk" isn't "Yes" or "No", but the fact that it will continue reading out 3 chars at a time saying "That's not yes or no"....

    --
    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. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Strangely enough, there's also a page on cpwiki about that, too. Clearing the input buffer, that is. http://cpwiki.sourceforge.net/Pause_console
    (The topic name's not quite right, though . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by matsp View Post
    How big the buffer should be is quite dependent on what you are asking for, right? If you expect the user to answer yes or no, you could get away with 4. If you are asking for a first name, you probably need about 20 to cover those lengthy double-names that are used in some places. For a full name (first + last), you are probably ok with 30-40 chars.

    What does make sense, however, is to write a function like dwks's, but also add a "clear the input buffer if too much input". There is of course nothing stopping a person from typing in "ajkhkfhkasdhfkasdhksdh" as the answer to "Yes or No" type question, and then you end up with "rubbish" in the input buffer. It's not really a problem that "ajk" isn't "Yes" or "No", but the fact that it will continue reading out 3 chars at a time saying "That's not yes or no"....
    OF course. It's subjective. Another matter might be to choose the right length of the buffer for the task. But that's another matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User vijoeyz's Avatar
    Join Date
    Jan 2008
    Posts
    3
    [QUOTE=omnificient;722179]I've been playing a bit with pointers and strings.
    Code:
    #include <stdio.h>
    
    int main()
    {
      char string;                        //Sets up a character named string.
      char * pString = &string;  //Makes a character pointer and assings the adress of string to it
      puts("Enter: ");
      scanf(" %s", pString);        //I know this could have been gets(), I used scanf().
     [snipped]

    I think (though I did not test), this program should fault, because you are reading input and storing at address of "string". You perhaps want to rewrite it as:

    Code:
    int main()
    {
      char string[SOME_NUM];                        //Sets up a character named string.
      char * pString = string;  //Makes a character pointer and assings the adress of string to it
      puts("Enter: ");
      scanf(" %s", pString);        //I know this could have been gets(), I used scanf().
     ...
    }
    Thanks,
    Vijay Zanvar
    http://faq.zanvar.in/category/pointers-and-arrays/

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And whilst it's likely that there are systems where a void * is different from a char *
    Not passing a pointer to void to printf with %p is always undefined behavior. If it for you on your particular implementation, fine, but I'll add the cast and be confident that there won't be problems.

    >I doubt the original poster's problem will be solved by a type-cast.
    Oddly enough, I wasn't trying to solve the OP's problem, I was correcting a mistake in one of the OP's examples.
    My best code is written with the delete key.

Page 2 of 2 FirstFirst 12
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, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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