initializing char arrays to null

This is a discussion on initializing char arrays to null within the C++ Programming forums, part of the General Programming Boards category; Hello again, I've found some good advice all over this board, but here's another, simpler, question I'm sure anyone can ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    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?

    thanks,
    James
    HB9/KC5ZFZ

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

    Safer:
    memset ( array, 0, SIZE );

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

    -Prelude
    My best code is written with the delete key.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > 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
    Posts
    9,796
    >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.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Prelude

    What warning does Lint give you?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    For this line
    char array[100] = {0};
    I get these warnings
    Code:
    Splint 3.0.1.1 --- 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)
    -Prelude
    My best code is written with the delete key.

  7. #7
    Unregistered
    Guest
    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, 04: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, 06:48 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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