simple question about a char

This is a discussion on simple question about a char within the C Programming forums, part of the General Programming Boards category; say I have the following: char c[20] = "adajhdqwuihdquiwh" and in the middle of the code I want to reset ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    simple question about a char

    say I have the following:

    char c[20] = "adajhdqwuihdquiwh"

    and in the middle of the code I want to reset c to an empty string again, how can I do this?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    c[0] = '\0';
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by vart View Post
    c[0] = '\0';
    That is the easiest way, but might not work for everything. It will make the first character '/0' which will make it seem like an empty string. But if later on you change the first character then you ll have your previous string with the first character changed. A more safe way is to do this:
    Code:
    for (i=0; i<20; ++i) {
       c[i] = '\0';
    }
    so all the characters of the string are "empty". If you empty the string then write something you will have a string with that something only. For example:
    Code:
    char c[20] = "adajhdqwuihdquiwh";
    c[0] = '/0'; //c[] is empty
    c[0] = 'b'; c[1] = 'e';
    printf("&#37;s", c); //outputs "ceajhdqwuihdquiwh" which isn't want you want
    on the other hand
    Code:
    char c[20] = "adajhdqwuihdquiwh";
    for (i=0; i<20; ++i) {
       c[i] = '\0';              //c[] is empty
    c[0] = 'b'; c[1] = 'e';
    printf("%s", c); //outputs "ce" which is what you want

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    c[0] = '/0'; //c[] is empty
    ^ is incorrect. '/0' are two chars, not a special char. And don't name it as '/0', because it's always, ALWAYS, '\0', no matter if it's linux, windows or mac.

    It's also easier to use memset instead of a loop. But what you do is actually pretty inefficient and not what most C programmers would do, I think.
    Still, choose the method that suits you best and stick with it.

    I'm siding with vart's method, however.
    Last edited by Elysia; 09-01-2008 at 03:18 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by C_ntua View Post
    But if later on you change the first character then you ll have your previous string with the first character changed.
    I'm not buying this. If you later change the first character, and only the first character, then it's because you have good reason to do so and you know what you are doing. I find that vart method is perfectly safe.
    I hate real numbers.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    This is fine for most applications:
    Code:
    c[0] = '\0';
    But if c stores something secret like a password and you don't want the risk of that information (or as least part of the information) to be found by accident, or by some hacker, you should nullify all the data like this:
    Code:
    memset( c, '\0', sizeof( c ) );

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by foxman View Post
    I'm not buying this. If you later change the first character, and only the first character, then it's because you have good reason to do so and you know what you are doing. I find that vart method is perfectly safe.
    But it is not perfectly safe, proven from some examples given. Lets say you work in a project and I tell you that this function empties a string. If you are an experience C programmer you would demand to know exactly what I mean by "empty string". But if not you will just assume that the string is empty. Which means that if you write one character you will have a string with one character. If you write two characters you will have a string with two characters.
    Now, many will argue that whenever you write a string in C you should always put the '\0' at the end. So, if we assume that everybody follows that rule then emptying only the first character is perfectly safe. But that is just an assumption. You never know how someone might want to manipulate a string so it better to assume the worst and empty the whole string.
    As for efficiency, it is obvious that is better to empty only the first char. But efficiency isn't everything.
    And I suppose memset() is better to use than a loop.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But by definition, [in C] a string is X characters plus a terminating zero. So any function that "empties a string" would just make X zero and put in a terminating zero. That is an empty string. And empty string is not "a sequence of X terminating zeros".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by C_ntua View Post
    ...But that is just an assumption. You never know how someone might want to manipulate a string so it better to assume the worst and empty the whole string.
    I seriously challenge that statement. Why?
    Because a function that receives something (especially which YOU haven't made), should not assume anything.
    If the function wants to do some special manipulation, then it should zero it out itself, and not put the work on you.
    As far as the function knows, when it finds a '\0', the string ends. And under normal circumstances, a receiving function only needs the string + its length. Then it can do whatever it wants with it.
    Using it as a buffer and not string is an entirely different matter, though, where 0ing might be a good thing. But again, that's not the discussion.

    As for efficiency, it is obvious that is better to empty only the first char. But efficiency isn't everything.
    C lives and dies by its efficiency, if you ask me. Especially since it can and is used to write speed critical code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    From what I've learned about mainframes over the past few years, they don't use NUL terminated strings. They use buffers of specific sizes... So if you're communicating with, or programming on a mainframe, you may need to blank out the whole string with NULs. But that is probably only true if you're copying the string with memcpy() rather than strcpy().

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    But then those aren't C-strings.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by robwhit View Post
    But then those aren't C-strings.
    You're right. But that's the best argument I could come up with (other than security) for why you might want to blank out the whole string.
    For most applications, just setting the first char to '\0' should be good enough, but there are certain times when you should be blanking out the whole string.

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Even though strings are assumed to end with '\0' that doesn't mean that everybody knows the correct terminology. The title says a "simple question about char" even though it asks how to empty a string. So of course I assume that the OP isn't familiar with what the correct definition of a string is in C. A lot of people thing a string is just an array of char.
    -So if the actual question is emptying an array of char then you should empty everything in the array (except if you want something more specific). As you would do if you wanted to empty an array of int.
    -If the actual question is emptying a string (a null terminated array of char) then you should just empty the first element.

    Depending how you see an array of char will give you the correct solution.

    A last thing is that when initializing an array of char as you did it is terminated by a '\0'. So in you case it will have 18 elements intialized. The 17 you put and the '\0' one.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by C_ntua View Post
    Even though strings are assumed to end with '\0' that doesn't mean that everybody knows the correct terminology.
    Then by no means, are they C programmers. C programmers know this.
    They may be students of the language, but not programmers of C.

    -So if the actual question is emptying an array of char then you should empty everything in the array (except if you want something more specific). As you would do if you wanted to empty an array of int.
    -If the actual question is emptying a string (a null terminated array of char) then you should just empty the first element.
    To both of those questions, the answer is the same. Set the first char to '\0'. Emptying a buffer is another matter and is closer to the solution you propose.
    Last edited by Elysia; 09-04-2008 at 02:26 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    >> A lot of people thing (sic) a string is just an array of char.

    But a string is an array of char in C. Those people would be right.

    Zeroing out memory can be done if you have a security concern, like cpjust suggests, and even then one could consider it a little overkill since RAM is not persistent memory.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Simple Proxy
    By Lina in forum C Programming
    Replies: 0
    Last Post: 04-01-2007, 01:36 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. question about functions and char *
    By Bittrexx in forum C Programming
    Replies: 4
    Last Post: 07-22-2003, 01:27 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 12:11 PM

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