Testing to see if something is chnull in Unix

This is a discussion on Testing to see if something is chnull in Unix within the C Programming forums, part of the General Programming Boards category; consider this definition: Code: #define chnull (char *)0 typedef struct { int month,day,year; }DATE; typedef struct Key KEY; typedef struct ...

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Question Testing to see if something is chnull in Unix

    consider this definition:
    Code:
    #define chnull (char *)0
      
    typedef struct 
      {
          int month,day,year;
      }DATE;
      
      typedef struct Key  KEY;
      typedef struct Key* KEYS;
    
      struct Key 
      {
          char *sender;
          char *addressee;
          char *regarding;
          DATE date;
          int id;
          char *fname;
          KEYS next;
      };
    if I set any of the char strings to chnull...
    Code:
    ...inside some function...
      //declare stuff 
      KEY temp;
    
      temp.fname=(char*)malloc(sizeof(20));
      temp.fname=chnull;
    I cannot successfully test for a null value by doing
    Code:
    if( ! temp.fname )
    nor by
    if( temp.fname==chnull )
    but I can test for a good value
    Code:
    if( temp.fname )
    So in this case, what is the proper way to test that
    temp.fname = chnull so that the test will evaluate to true?

    Thanks in advance,
    Mike
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    I take it you're trying to generate an empty string

    Well then
    > #define chnull (char *)0
    #define chnull '\0'

    > temp.fname=(char*)malloc(sizeof(20));
    > temp.fname=chnull;
    temp.fname=malloc( sizeof(char) * 20 );
    temp.fname[0]=chnull;
    1. sizeof(20) is sizeof(int) - 4 usually, and certainly not 20
    2. don't cast the result of malloc - make sure you include stdlib.h - this isn't C++

    > if( temp.fname==chnull )
    if ( temp.fname[0] == chnull )

  3. #3
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    I can't change the define. This is a project where changing
    the defines are not allowed.


    I did make the change on the unix machine to test for chnull.
    if ( temp.fname[0] == chnull )
    the compiler complained. I got a warning:

    bash-2.03$ cc -c -g search.c
    cc: Warning: search.c, line 82: In this statement, "ki.fname[0]" of type "signed char", is being converted to "pointer to signed char". (cvtdifty
    pes)
    if(ki.fname[0]==chnull && ki.id==0)
    ---------------------------^
    bash-2.03$

    So I changed it to ki.fhame[0]==*chnull;
    the compiler didn't complain, but I (still) crash when I execute the line
    of code that contains that expression.

    thanks
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > I can't change the define.
    Oh, one of those problems

    Well, in reference to your original post then...

    You initialise the pointer to chnull, then presumably at some later time actually allocate some memory

    temp.fname=chnull;
    ....
    temp.fname = malloc( 20 );

    I can't see anything wrong with your if statements

    if( ! temp.fname )
    if( temp.fname==chnull )
    Both these are true, if temp.fname is chnull

    if( temp.fname )
    if( temp.fname != chnull )
    These are true if temp.fname != chnull

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 09:54 AM
  2. Setting up a Unix box
    By @nthony in forum Tech Board
    Replies: 6
    Last Post: 07-22-2007, 10:22 PM
  3. UNIX (Linux, BSD, etc) Programming :: UNIX
    By kuphryn in forum Linux Programming
    Replies: 6
    Last Post: 04-01-2004, 07:44 PM
  4. Unix Sockets
    By prvindia in forum Linux Programming
    Replies: 5
    Last Post: 03-11-2003, 08:16 AM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM

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