Thread: looking at the string of characters in buffer for fgets

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,727
    You scored 1 out of 3. Fail!
    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

  2. #17
    Registered User
    Join Date
    Apr 2019
    Posts
    281
    one the first line (function deceleration) you declared the argument as int in main you declared the array as int (int xs[].....) so one can assume unless they aren't part of main that xs[0] or xs or &xs are of the same type surely
    coop

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,727
    Ah, perhaps we have found a serious misunderstanding. In this function definition:
    Code:
    void foo(int *p) {}
    The type of the parameter named p is not int. It is pointer to int, i.e., int*

    In this declaration and initialisation of an array named xs:
    Code:
    int xs[] = {1, 2};
    The type of xs is array of 2 ints, i.e., int[2]. The 2 ints is because of the initialisation to an aggregate containing 2 ints.

    For foo(xs[0]), the type of the argument is an int. You know that foo expects an int*, so this is wrong.

    For foo(xs), the type of the argument is int[2]. This can be converted to int* since when passed as an argument, an array is converted to a pointer to its first element, so this is correct.

    For foo(&xs), the type of the argument is pointer to the array of 2 ints itself, i.e., int(*)[2]. Hence, this is wrong, even though the address would coincide with the address of the first element of xs.

    Function pointers aside, it is never ever the case that xs[0], xs, and &xs have the same type.
    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

  4. #19
    Registered User
    Join Date
    Apr 2019
    Posts
    281
    ok so if i had an array
    Code:
     int array[5] = {1,2,3,4,5}
    and i wanted my function to only change the 3rd element i would have to pass the entire array plus another argument to the element i wanted it to change?

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,727
    No. Pass a pointer to the 3rd element.
    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

  6. #21
    Registered User
    Join Date
    Apr 2019
    Posts
    281
    ok im not getting this
    i thought you just said you couldn't do that

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,727
    Where did I say you couldn't do that?
    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

  8. #23
    Registered User
    Join Date
    Apr 2019
    Posts
    281
    i think i get it now
    Code:
    int array[5] = {1,2,3,4,5}, int *p_array = array; // p_array points at element [0] ie 1
    p_array + 2;// p_array now points at element [2] ie the 3
    foo(p_array)
    i re-read your first comment about xs[0] being wrong.... i keep confusing arrays becoming pointers and indexing pointers to arrays
    Last edited by cooper1200; 1 Week Ago at 03:48 AM.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,727
    Aye, so foo(xs[0]) is wrong, but foo(&xs[0]) is correct and equivalent to foo(xs). For passing a pointer to the 3rd element, you could write foo(&array[2]), or more simply foo(array + 2).
    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

  10. #25
    Registered User
    Join Date
    Apr 2019
    Posts
    281
    while we are on the subject of pointers in general which is correct int* my array or int *myarray i did read a thread on here somewhere that argued the case for both

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,727
    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

  12. #27
    Registered User
    Join Date
    Apr 2019
    Posts
    281
    thats the page i read. the reason i am learning c is to get a good grounding then i want to learn c++ (classes etc) then get into embedded systems and the like is learning int *array going to trip me up

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,727
    Nope, it's just subjective style.
    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

  14. #29
    Registered User
    Join Date
    Apr 2019
    Posts
    281
    i got it to compile with no warnings or errors but it still doesn't do what i want it to
    Code:
    void get_player_names(int num_of_players, Hand *p_player)
    {
        int i, x, str_index = 0;
        char buff[2], str_temp[10];// *p_str_temp = str_temp;// *p_buff = buff;
    
        for (i = 0; i < num_of_players; i++)
        {
            printf("Please enter player %d's name: ", i + 1);
            x = 0;
            str_index = 0;
            while (x != 1)
            {
                fgets(buff, sizeof(buff), stdin);
                if (!strcmp(buff, "\n"))
                {
                    strcpy(&str_temp[str_index], &buff[0]);
                    str_index++;
                }
                else
                {
                    strcpy(&str_temp[str_index], &buff[1]);// add the /0 onto the end of str_temp
                    flush_line();//get rid of the "\n"
                    x = sscanf(str_temp, "%s", p_player[i].player_name);
                    if (x != 1)
                    {
                        printf("Incorrect Entry\n Please enter player %d's name: ", i + 1);
                        x = 0;
                    }
                }
            }
        }
        for (i = 0; i < num_of_players; i++)
        {
            printf("Player %d's name is %s\n", i, p_player[i].player_name);
        }
    }

  15. #30
    Registered User
    Join Date
    Apr 2019
    Posts
    281
    lets say the first players name is tom and the seconds is josie i want it to assign "tom" then move onto the next one without waiting for more input and therefore overwriting tom

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is fgets not fully populating its buffer?
    By ImageJPEG in forum C Programming
    Replies: 6
    Last Post: 12-27-2017, 09:18 AM
  2. fgets skipping, buffer issue?
    By TLW in forum C Programming
    Replies: 2
    Last Post: 02-20-2011, 08:15 PM
  3. Excess buffer using fgets
    By $l4xklynx in forum C Programming
    Replies: 20
    Last Post: 06-25-2009, 03:56 AM
  4. fgets buffer
    By Pythonsnake in forum C Programming
    Replies: 8
    Last Post: 01-11-2006, 12:27 AM
  5. fgets(buffer,sizeof(buffer),stdin);
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 10-28-2003, 10:41 AM

Tags for this Thread