Jump to content

The Truth About Nulls


Recommended Posts

Well, I've seen NULL represented as \0 by not only Markie, my coding idle, but by text book writers too.

 

 

The NULL symbol is actually simply a 0. In C, the \ is used to indicate special characters, such as ", \, and NULL. So, putting a quote in a string would require the use of a \". Simple?

 

 

Now, I would like to say that a NULL is not read as \0, but as a 0. The \ indicates that it's special(Edward), and declares it as NULL. So, if you ever do any programming in Assembly, and you put a string in your .data section, it had better look like:

 

message db "Blah Blah Blabbity Blah",0

 

And not:

 

message db "Blah Blah Blabbity Blah",\0

 

If you can assemble the code in the second block, then your Assembler probably has something to correct your mistake.

 

 

And, because it has being said so by x86 Assembly, it is so.

 

 

 

 

Sorry, but it's been bugging me lately... thank ye for reading this and understanding that a NULL is NOT \0, but 0. Thanks.

Share this post


Link to post
Share on other sites

I think you're a little mixed up to be honest.

 

\0 in C is just a... convention, I suppose. (great, looks like I have been disconnected AGAIN, whats up with my internet today!!!!)

 

You couldn't end a string (as in, the strings enclosed in "", you can of course when working with them as arrays) in C by just using a 0, because it'd be interpretted as a literal 0, as in.. an integer, and you'd just end up with a 0 on the end of your string. I'm sure you don't really want that! Just like you couldn't start a new line by saying "n". It's \n and \0 for doing such things with strings. This isn't to say that NULL isn't 0 in C. I think NULL is defined as 0 which has been typecasted to a void pointer in some cases and 0 in others *goes to look at header files*

 

/* A null pointer constant.  */

#if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL  /* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else   /* G++ */
#ifndef __cplusplus
#define NULL ((void *)0)
#else   /* C++ */
#define NULL 0
#endif  /* C++ */
#endif  /* G++ */
#endif	/* NULL not defined and <stddef.h> or need NULL.  */
#undef	__need_NULL

 

Perhaps try it for yourself, so you can see that \0 is just a kind of convention thing. It's just an 'easier to follow' type convention.

 

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

int main() {
   // string example
   char *str1;
   char *str2;
   char *str3;
   
   str1 = (char *)malloc(10 * sizeof(char));
   str2 = (char *)malloc(10 * sizeof(char));
   str3 = (char *)malloc(10 * sizeof(char));
   
   assert(str1 != NULL);
   assert(str2 != NULL);
   assert(str3 != NULL); 
   
   memset(str1, 1, (10 * sizeof(char)));
   memset(str2, 1, (10 * sizeof(char))); 
   memset(str3, 1, (10 * sizeof(char)));    
     
   // print strings before terminating
   printf("Before terminating half way through the string:\n");
   printf("---\n");
   printf("str1: %s\n", str1);
   printf("str2: %s\n", str2);
   printf("str3: %s\n\n", str3);        
   
   str1[5] = '\0';
   str2[5] = NULL;
   str3[5] = 0;

   printf("After terminating half way through the string:\n");  
   printf("---\n");  
   printf("str1 (\\0): %s\n", str1);
   printf("str2 (NULL): %s\n", str2);
   printf("str3 (0): %s\n\n", str3);        

     
   printf("NULL comparisons\n");
   printf("---\n");    
   // comparison example
   if ('\0' == NULL) {
      printf("\\0 is NULL\n");      
   }   
   
   if (0 == NULL) {
      printf("0 is NULL\n");      
   }
   
   if (0 == '\0') {
      printf("0 is \\0\n");      
   }
   
   printf("\nOn this fine morning, NULL = %d\n", NULL);
       
   system("pause");
}

 

Hope you can see what I am getting at...

 

If you can't,

 

   str1[5] = '\0';
   str2[5] = NULL;
   str3[5] = 0;

 

from that, which looks like the most sensible to use as an assignment to end a string? To me, since you're working with an array of characters (in this case), it's most sensible and easiest to read for other people to assign a character (i.e the '\0') in to the array, not an integer. See?

 

 

I'll hit add reply as soon as my internet stops being a complete bastard :ph34r:

 

and now, if you'd be so kind as to punch me in the jaw repeatedly please? It made a horrible noise last night while I was eating and now it hurts like buggery, feels like it's .. misaligned itself :)

 

*punches modem in the jaw too* grrrrrrrrr

 

(mrboo|bone)/home/mrboo$ man null
Formatting page, please wait...Done.
NULL(4)                FreeBSD Kernel Interfaces Manual                NULL(4)

NAME
    null -- the null device

DESCRIPTION
    The null device accepts and reads data as any ordinary (and willing) file
    - but throws it away.  The length of the null device is always zero.

FILES
    /dev/null

SEE ALSO
    zero(4)

HISTORY
    A null device appeared in Version 7 AT&T UNIX.

FreeBSD 4.10                     June 5, 1993                     FreeBSD 4.10

 

:) Just thought you might like that one lol

Share this post


Link to post
Share on other sites

I was tired when writing this, so I forgot to point out that the 0 in general typing isn't null, but 00110000 in ASCII.

But you make a good point... from what I can understand in my delerium.

479425[/snapback]

 

In ASCII, yes, it probably is. I don't know ASCII bit codes and I am not really prepared to find out until I need to know them :) As an integer say, int something = 0 or... somechar[4] = 0 (not to be confused with somechar[4] = '0', that's totally different), it should be represented as.... 0000 0000 0000 0000 0000 0000 0000 0000 (32 bits, int = 4 bytes..) char is only 1 byte, so that's 8 bits.. so that'd just be 0000 0000 for 0

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...