Thread: initializing array .

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brafil
    NULL is "(void *)0", NUL is "(char)0".
    NULL is an implementation defined null pointer constant, so whether it is 0 or (void*)0 is implementation defined. I do not think that the name NUL is actually defined in standard C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by transgalactic2 View Post
    Code:
    char arr[256]={0};
    so if want to initialize arr without a loop
    i can put only NULL char in them
    so this is correct
    ?

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Apparently not... see laserlights post below...
    Last edited by Bladactania; 04-20-2009 at 09:18 AM. Reason: Oops

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    so this is correct
    No. I already told you that you can use an initialiser to initialise the chars to something other than the null character, though it may be tedious to do so by hand. Stop talking about "NULL char" since it can be confused with the NULL macro, which would then obfuscate your intending meaning.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in the start matsp said that it

    works
    Code:
    char arr[256]={0};

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    in the start matsp said that it
    Define "works"?

    You say that you want to initialise the elements of the array to something other than the null character, then you say that an example that initialises the elements of the array to a null character "works"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Zero yes, NULL no. Leave NULL out of this, not even in the ballpark.

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i understand that other values dont works

    but this value(i cant decide its name)
    Code:
    char arr[256]={0};
    does puts in every cell 0

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    but this value(i cant decide its name)
    It is an int literal 0, so you can call it 0, or you can call it a null character since char is the destination type.

    Quote Originally Posted by transgalactic2
    does puts in every cell 0
    That is because arr[0] is initialised to 0, then arr[1] to arr[255] are zero initialised.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 12:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 05:19 PM