Can someone give me an example of the following

This is a discussion on Can someone give me an example of the following within the C Programming forums, part of the General Programming Boards category; The following comment was made by one of the amazon dot com engineers on my schools internal bbs system. The ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Can someone give me an example of the following

    The following comment was made by one of the amazon dot com engineers on my schools internal bbs system.

    The return value of malloc compares equal to zero upon failure.

    The ctype functions return nonzero (true)
    if and only if the value of the argument conforms to that
    in the description of the function.

    If you want the return value
    to be able to indicate which mode of failure occured,
    then that's when zero is the value to return for success.
    Which ctype functions is he talking about?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    Perhaps isalpha() and company?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    He is talking about the family of functions that are declared in the ctype.h header file.

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Ohh..... okay. Thanks.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    For example if malloc returns 0 (or better, NULL) it failed. If isdigit returns 0, it failed (that means the argument is not a digit).

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > malloc returns 0
    Only in C++ :-).

    Quote Originally Posted by ISO/IEC 9899 - 7.17
    The macros are NULL ... which expands to an implementation-defined null pointer constant
    Last edited by zacs7; 05-31-2009 at 06:31 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    Quote Originally Posted by zacs7
    Only in C++
    No, the behaviour is the same for C as well, though it would be more accurate to talk about a null pointer being returned.

    EDIT:
    Oh, you were trying to be pedantic. Unfortunately for you, 0 is a null pointer constant in both C and C++. However, what is returned by malloc() would be a null pointer of type void*, if it is not a pointer to allocated memory.
    Last edited by laserlight; 05-31-2009 at 06:33 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    My point was, in C, NULL != 0 (well, it doesn't have to be).

    I've heard of such a system where NULL was not at "address 0". Myth perhaps, but I was told it made testing rather hard.

    [edit]
    Hmm, it seems
    ISO/IEC 9899 - 6.3.2.3 Pointers says: "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant."

    So perhaps I was wrong, sorry :-). I guess you shouldn't listen to other people without first reading the standard for yourself
    [/edit]
    Last edited by zacs7; 05-31-2009 at 06:38 AM.

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    NULL = 0 (with an implicit cast in C). Often it is 0.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    My point was, in C, NULL != 0 (well, it doesn't have to be).
    Well, according to the Standard it does:

    Quote Originally Posted by ISO/IEC 9899
    6.3.2.3
    3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
    Since NULL is a macro that expands to the null pointer constant, NULL must be 0. Contra zacs7 and brafil, it is not implementation dependent.

    [edit: laserlight caught my failure to read zac7's entire post. At least we are obviously on "the same page" with this, ha ha.]
    Last edited by MK27; 05-31-2009 at 09:28 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    Quote Originally Posted by MK27
    Well, according to the Standard it does:
    To be fair, zacs7 quoted the same part of the C standard in the edit to the post that you quoted.

    Quote Originally Posted by MK27
    Since NULL is a macro that expands to the null pointer constant, NULL must be 0. Contra zacs7 and brafil, it is not implementation dependent.
    It is implementation dependent as to exactly which null pointer constant NULL expands to (i.e., the type can differ). zacs7 cited the correct part of the C standard that states as such.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    The standard guarantees that NULL doesn't point to any object or function and is not a legal pointer. That means undefined behavior when deferencing NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 10:38 AM
  2. Can you give me your tip plz :)
    By dionys in forum C Programming
    Replies: 6
    Last Post: 04-11-2004, 11:14 PM
  3. Can you give me an example of a new line character?
    By Golffor1 in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2003, 11:59 AM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM
  5. Just to give you an idea of what we're going through...
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-13-2001, 02:09 PM

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