Thread: Looking for null terminator

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Looking for null terminator

    When working with a character array:

    Code:
    char name[256] = "";
    I normally check to see if it is a null string by:
    Code:
    if (name[0] == '\0') ...
    or I match along the string looking for '\0' when I parse strings some times.

    at times I might even do this:
    Code:
    switch (name[i])
    {
    case 'A':
    ...
    ...
    case 'B':
    ...
    ...
    }
    Question is, if now I declare name[256] as:
    Code:
    Tchar name[256]=L"";
    how should my code be changed accordingly, if I will be creating the app for use in both char and wchar_t cases?

    Thanks,

    MC

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    It will be the same for both char and widechars. What matters is the define UNICODE.

    The only things you should worry about is the size of your variable getting bigger. Wrapping all your string in _TEXT(). using %S identifeir for wide chars and %s for chars(UNICODE not defined).


    fine on both char and widechar
    Code:
    if (name[0] == '\0')
    Code:
    switch (name[i])
    {
    case 'A':
    ...
    ...
    case 'B':
    ...
    ...
    }

    *edited

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Thanks, just to make sure I don't need to do:

    Code:
    switch (name[i])
    {
    case L'A':
    ...
    ...
    case L'B':
    ...
    ...
    }
    Correct?

    MC

    Quote Originally Posted by loko
    It will be the same for both char and widechars. What matters is the define UNICODE.

    The only things you should worry about is the size of your variable getting bigger. Wrapping all your string in _TEXT(). using %S identifeir for wide chars and %s for chars(UNICODE not defined).


    fine on both char and widechar
    Code:
    if (name[0] == '\0')
    Code:
    switch (name[i])
    {
    case 'A':
    ...
    ...
    case 'B':
    ...
    ...
    }

    *edited

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by MiamiCuse
    Thanks, just to make sure I don't need to do:

    Code:
    switch (name[i])
    {
    case L'A':
    ...
    ...
    case L'B':
    ...
    ...
    }
    Correct?

    MC
    Just play it safe and do
    Code:
    switch (name[i])
    {
    case _T('A'):
    ...
    ...
    case _T('B'):
    ...
    ...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminator 4
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-23-2009, 11:15 PM
  2. Removing null terminator from a string
    By Canadian0469 in forum C Programming
    Replies: 3
    Last Post: 10-27-2007, 02:03 PM
  3. Terminator 3..nope
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 07-18-2003, 07:42 AM