Thread: Help with strcpy() in multidimensional linked list

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c.user
    you think isdigit('0') should be incorrect ? '0' will be signed
    However, it is obvious that the value of '0' is an int that is representable as an unsigned char.
    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 2009
    Location
    Russia
    Posts
    116
    but it should be casted while the pass to isdigit (prototype rule) and then C99 7.4.1 undefined behavior

    it has greater than zero code in ASCII, so any symbol of 7-bit ASCII should not casted hence
    Last edited by c.user; 05-24-2009 at 01:05 AM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c.user
    but it should be casted while the pass to isdigit (prototype rule) and then C99 7.4.1 undefined behavior
    What do you mean? Since the value is known to be within the range specified by the pre-condition, there is no need to cast to unsigned char and back to int. cas' point was that since the contents of the dollars array indirectly comes from user input, it cannot be guaranteed that the value of a given char from that array will satisfy the pre-condition, hence a cast to unsigned char is necessary to force satisfaction of the pre-condition and thus avoid undefined behaviour.
    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 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by laserlight
    the contents of the dollars array indirectly comes from user input
    you think function can show a digit on a non-digit character ? if this range sticked (maybe it can change for the codes, but they will be greater than zero) isdigit will know only the digits, and for the one character like dollars[0] it will find only one match, how it will be undefined for this situation ? how it can ?

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c.user
    you think function can show a digit on a non-digit character ? if this range sticked (maybe it can change for the codes, but they will be greater than zero) isdigit will know only the digits, and for the one character like dollars[0] it will find only one match, how it will be undefined for this situation ? how it can ?
    It will be undefined because the Standard says so, even if it returns the value you expect for every standard library implementation in existence, simply because I can write a standard library implementation that does what you do not expect and yet is still standard conforming.
    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 2009
    Location
    Russia
    Posts
    116
    How can you write it for the one char from dollar[0] ?
    It must return that non-digit is a digit (error like)

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c.user
    How can you write it for the one char from dollar[0] ?
    It must return that non-digit is a digit (error like)
    For example:
    Code:
    int isdigit(int c)
    {
        return (c >= '0' && c <= '9') || (c < 0 && c != EOF);
    }
    So, assuming ASCII and that EOF is -1, and fgets() manages to read -2 converted to a char (where char is signed), and then that char happens to be placed into dollar[0], then isdigit(dollar[0]) will return 1, even though the character with value of -2 is not a digit. Some of the other character input functions are immune to this since they read characters as unsigned char converted to int, but as far as I can tell fgets() is not required to 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 2009
    Location
    Russia
    Posts
    116
    I got one segmentation fault when I use int type for the array where fgets save
    but when I changed it to signed char, it's ok

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    {
        signed char c[3];
        
        printf("%c\n", -2);
        fgets(c, 3, stdin);
        
        printf("%c\n", *c);
        printf("%d\n", isdigit(*c));
        return 0;
    }
    you know why it doesn't print a true ? because every value saved in every char by fgets, then the check checks for only one char; digits (their codes) are sticked obviously, when he takes the dollar[0], he takes only one chararacter (8 bits in this case), also digits places only once in the table (every digit has it's own place and only one) and he can't take -207 from one char (it can include numbers from -127 to up only)
    How did you get the true for non-digit ?
    Last edited by c.user; 05-24-2009 at 09:05 PM.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop doing stupid stuff, and compile with warnings on:
    cuser1.c:5: warning: return type defaults to `int'
    cuser1.c: In function `main':
    cuser1.c:9: warning: pointer targets in passing arg 1 of `fgets' differ in signedness
    Didn't you read the part about "stop trying to user YOUR setup to prove if something's standard"?


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

  10. #25
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
    #include <stdio.h>
    
    main()
    {
        unsigned char line1[100];
        signed char line2[100];
        char line3[100];
        
        fgets(line1, sizeof line1, stdin);
        fgets(line2, sizeof line2, stdin);
        fgets(line3, sizeof line3, stdin);
        
        return 0;
    }
    Code:
    [guest@station src]$ cc -Wall *.c -o test
    main.c:5: предупреждение: по умолчанию возвращаемый тип функции - ‘int’
    main.c: In function ‘main’:
    main.c:10: предупреждение: pointer targets in passing argument 1 of ‘fgets’ differ in signedness
    main.c:11: предупреждение: pointer targets in passing argument 1 of ‘fgets’ differ in signedness
    [guest@station src]$ ./test
    one
    two
    three
    [guest@station src]$
    Code:
    7.19.7.2  The fgets function
    
           Synopsis
    
           [#1]
    
                   #include <stdio.h>
                   char *fgets(char * restrict s, int n,
                           FILE * restrict stream);
    
           Description
    
           [#2]  The  fgets  function  reads  at most one less than the
           number of characters specified by n from the stream  pointed
           to  by stream into the array pointed to by s.  No additional
    
           ____________________
    
           232An end-of-file and a read error can be  distinguished  by
              use of the feof and ferror functions.
    it's from C99 standard but in C89 there is no restrict only, so, quzah, it doesn't matter what you see

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread