Thread: initializing array .

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    initializing array .

    Code:
    char arr[256]={'0'};
    it puts '0' in every cell
    ??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No. And I'm pretty sure we've talked about this before.

    --
    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.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i remember that we could put in every cell of an array one value
    by a similar way
    ??

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Listen to matsp. You can find dozens of pages explaining this in google.

    PS: Really sure it's '0' and not 0?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i remember that we could put in every cell of an array one value
    by a similar way
    ??
    Only to the value zero (not '0', that's the character representing the digit zero, commonly the value 48, but not guaranteed). For any constant other then zero, you will have to either enter all the 256 values [16 rows of 16, for example], or write code to fill in the values - e.g. use memset().

    --
    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.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    actually it was 0
    but i though it was '0' because its a array of chars

    not an array of integers

    ??

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant put 0 into char array
    its a number
    ??

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i cant put 0 into char array
    its a number
    Then you can't put '0' in a char array either, since it is also a number (of type int, in fact).
    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

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the ascii value of 0 is NULL
    so it puts NULL in every cell

    and i can do this shortcut
    only with NULL value

    can i do
    Code:
    char arr[256]={67};//the letter that 67 represenst

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by laserlight View Post
    Then you can't put '0' in a char array either, since it is also a number (of type int, in fact).
    '0' is a char
    not a number

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    the ascii value of 0 is NULL
    so it puts NULL in every cell
    The null character has a value of 0, and this is true with ASCII as well. However, the macro NULL is different in meaning from the null character, even though they are equal and may have the exact same value and type. Consequently, it is usually semantically wrong to say that "it puts NULL in every cell" unless you are talking about an array of pointers.

    Quote Originally Posted by transgalactic2
    and i can do this shortcut
    only with NULL value
    This "trick" works with zero values, including NULL, because those elements of the array that are not explicitly initialised will then be zero initialised.

    Quote Originally Posted by transgalactic2
    can i do
    Yes, but you will end up with an array for which arr[0] has the value of 67 and the rest have the value of 0.

    EDIT:
    Quote Originally Posted by transgalactic2
    '0' is a char
    not a number
    No, '0' is an int, and the char type is an integer type.
    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

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    ya i just tested it works with
    Code:
    char arr[256]={67}
    but fills only arra[0] not all array other will be NULL

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Code:
    char arr[256]={0};
    so if want to initialize arr without a loop
    i can put only NULL char in them

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could simply write out 67 256 times

    Or you could use memset().
    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

  15. #15
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Maybe even a for-loop, but memset is often more optimized. Or, if you don't mind a trailing NUL:

    Code:
    char arr[] = "00000000000000000000000000000000000000000000000"...;
    BTW: NULL is "(void *)0", NUL is "(char)0".

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