checking for null

This is a discussion on checking for null within the C Programming forums, part of the General Programming Boards category; Can someone tell me how do i check for null/empty values ? Code: fgets(name, sizeof(name), stdin) == NULL Does null ...

  1. #1
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80

    checking for null

    Can someone tell me how do i check for null/empty values ?

    Code:
     fgets(name, sizeof(name), stdin) == NULL
    Does null stands for empty value ? is it returned if the user keyed enter only ? coz i tried

    Code:
         
         if (fgets(name, sizeof(name), stdin) == NULL) 
         {
             printf("no value\n"); 
         }
    to see how it works, but it didn't print aniting . It just skipped to the next part of my program. Did i get the logic wrong or something ?
    /* Have a nice day */

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    fgets() won't return NULL if the user pressed ENTER. It will store "\n" in the buffer. fgets() will return NULL when EOF is reached and no characters have been read, or when there's an error. It's all explained very clearly in the man page:
    Code:
           gets() and fgets() return s on success, and NULL on  error
           or  when  end of file occurs while no characters have been
           read.
    If you want to see if the user just pressed ENTER then this check should suffice:
    Code:
    if(fgets(buf, sizeof(buf), stdin) == NULL)
    {
      // End of file or error
    }
    else if(*buf == '\n' || *buf == '\0')
    {
      // User pressed enter
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    itsme86 is the *buf a dereference pointer or something to show the value in it ? in this case its either \n or \o ? i didn't variables can store escape sequences.
    /* Have a nice day */

  4. #4
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    itsme86 is the *buf a dereference pointer or something to show the value in it ? in this case its either \n or \o ? i didn't know variables can store escape sequences.
    /* Have a nice day */

  5. #5
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Quote Originally Posted by rEtard
    Does null stands for empty value ?
    Implementing the macro NULL simply requires that you choose the most suitable of several possible options 0, 0L, or (void *)0. You pick a form that works properly as an argument of type pointer to void (or pointer to char, signed char, or unsigned char) in the absence of a function prototype. It might be more elegant, perhaps, to include a null-pointer constant in the C language proper.

    The macro NULL serves as an almost-universal null pointer constant. You use it as the value of a data-object pointer that should point to no data object declared (or allocated) in the program. The macro can have any of the definitions 0, 0L, or (void *)0. The last definition is compatible with any data object pointer. It is not, however, compatible with a function pointer.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by rEtard
    itsme86 is the *buf a dereference pointer or something to show the value in it ? in this case its either \n or \o ? i didn't know variables can store escape sequences.
    *buf is the same as saying buf[0]. It's the first element in the array.
    If you understand what you're doing, you're not learning anything.

  7. #7
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    itsme86 Alrite, great. Thanz, now i got it.
    Stack Overflow so you're saying i can define "null values" ? sorrie if i got it wrong, somehow its too advanced for me to handle. I don't understand the use of macros and what they r either. I think i shld check them out now.
    /* Have a nice day */

  8. #8
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello again,

    Quote Originally Posted by rEtard
    itsme86 is the *buf a dereference pointer or something to show the value in it ? in this case its either \n or \o ? i didn't know variables can store escape sequences.
    A character constant is an integer, written as one character within single quotes, such as 'x'. The value of a character constant is the numeric value of the character in the machine's character set. Certain characters can be represented in the character and string constants by escape sequences like \n (newline); these sequences look like two characters, but represent only one. In addition, an arbitrary byte-sized bit pattern can be specified by:

    '\ooo'
    where ooo is one to three octal digits (0...7).

    The complete set of escape sequences is:
    Code:
    Sequence Definition
    \a		alert (bell) character
    \b		backspace
    \f		formfeed
    \n		newline
    \r 		carriage return
    \t		horizontal tab
    \v		vertical tab
    \\		backslash
    \?		question mark
    \'		single quote
    \"		double quote
    \ooo		octal number
    \xhh		hexadecimal number
    The character constant '\0' represents the character with value zero, the null character. '\0' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0.

    Quote Originally Posted by rEtard
    Stack Overflow so you're saying i can define "null values" ? sorrie if i got it wrong, somehow its too advanced for me to handle. I don't understand the use of macros and what they r either. I think i shld check them out now.
    NULL is already defined in the standard C library. The NULL macro can represent 0, 0L, or (void *)0. Character arrays contain '\0' at the end of the array to mark the end of the string of characters.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

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