Thread: initializing char arrays to null

  1. #1
    Registered User
    Join Date
    Feb 2002

    Question initializing char arrays to null

    Hello again,

    I've found some good advice all over this board, but here's another, simpler, question I'm sure anyone can answer.

    I'm using borland 5 and i can't seem to set my char arrays to the null character.

    I've tried
    char name[10]=/0;
    char name[10]=\0;
    char name[10]=NULL;

    but that only returns an error, what's the correct syntax?


  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    You can use a tricky technique or a safer method.
    char array[SIZE] = {0};

    memset ( array, 0, SIZE );

    Memset requires that you include either string.h or string, but it won't give warnings on some compilers.

    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > char array[SIZE] = {0};
    Nothing wrong with this

    It just assumes that the remaining SIZE-1 elements are also zero, so that's it, job done

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >Nothing wrong with this
    If there's nothing wrong with it then why did someone bother to make Lint give a warnng for it?

    I've found that it causes no problems or unusual output, but I obssessively clear Lint warnings, so I cosider memset safer. This is one of those situations where it's a matter of personal preference.

    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2002

    What warning does Lint give you?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    For this line
    char array[100] = {0};
    I get these warnings
    Splint --- 08 Jan 2002
    cTest.c: (in function main)
    cTest.c(6,20): Initializer block for array has 1 element, but declared as char[100]: 0
      Initializer does not define all elements of a declared array. (Use
      -initallelements to inhibit warning)
    cTest.c(6,21): Initial value of array[0] is type int, expects char: 0
      Types are incompatible. (Use -type to inhibit warning)
    My best code is written with the delete key.

  7. #7
    you can also try these to initialize just the first element of the array to null:

    char buffer[10];
    buffer[0] = '\0';//pictoral null char, need the single quotes though

    buffer[0] = 0;//the integer zero when assigned to char is null char,
    the quotes and backslash escape char just make it abundantly clear that you know what you are trying to do.

    buffer[0] = NUL;//yup, one L instead of two. I believe NUL is typedefed to 0 or '\0', whichever you prefer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM