Thread: some queries on arrays

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    some queries on arrays

    Hi

    1: Is this possible to have an array of zero size such as array[0]? What would it mean?

    2: The instructor mentioned that it is not possible to return an array using a user-defined function (perhaps, this is true in the context of the techniques available to the beginners). I think he was also saying that it is possible to return one element of an array at a time.

    3: Suppose
    Code:
    int LEN = 10
    , then when I write
    Code:
    char name[LEN] = {0}
    what does it really mean? The name array is supposed to contain characters but I'm using a constant value which is "0". All the elements of the array have been initialized to "0".
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    2. You may be enlightened by
    Code:
    typedef int foo[32]; //give a name to an array of 32 ints
    
    foo function_that_returns_an_array(void);
    
    int main() {
        foo bar;
        return 0;
    }
    3. If you only declare LEN as an int, then "char name[LEN]" is an error. LEN must be a constant to be used as the length of an array. Also, if you haven't realized yet that characters are numbers, then the last who-knows-how-long have been a waste.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    1: Is this possible to have an array of zero size such as array[0]? What would it mean?
    No.

    2: The instructor mentioned that it is not possible to return an array using a user-defined function (perhaps, this is true in the context of the techniques available to the beginners). I think he was also saying that it is possible to return one element of an array at a time.
    It is true in all cases, so long as array means array type. Some functions will return a pointer to an element in an array, and that pointer could access the rest of the array. It is always possible to return a single element in an array.

    3: Suppose
    Code:
    int LEN = 10
    , then when I write
    Code:
    char name[LEN] = {0}
    what does it really mean? The name array is supposed to contain characters but I'm using a constant value which is "0". All the elements of the array have been initialized to "0".

    Consider instead:
    Code:
    char name[LEN] = {'M','e'};
    This syntax initializes the array's first two characters to 'M' and 'e', and the rest of the array is 0-initialized. Generally any members of the array that you don't specify a value for are initialized to 0, so long as at least one character is initialized.
    Last edited by King Mir; 06-21-2011 at 04:33 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by King Mir View Post
    1: Is this possible to have an array of zero size such as array[0]? What would it mean?
    No.

    2: The instructor mentioned that it is not possible to return an array using a user-defined function (perhaps, this is true in the context of the techniques available to the beginners). I think he was also saying that it is possible to return one element of an array at a time.
    It is true in all cases, so long as array means array type. Some functions will return a pointer to an element in an array, and that pointer could access the rest of the array. It is always possible to return a single element in an array.

    3: Suppose
    Code:
    int LEN = 10
    , then when I write
    Code:
    char name[LEN] = {0}
    what does it really mean? The name array is supposed to contain characters but I'm using a constant value which is "0". All the elements of the array have been initialized to "0".

    Consider instead:
    Code:
    char name[LEN] = {'M','e'};
    This syntax initializes the array's first two characters to 'M' and 'e', and the rest of the array is 0-initialized. Generally any members of the array that you don't specify a value for are initialized to 0, so long as at least one character is initialized.
    Thank you, tabstop, King Mir.

    That would mean the beginning of the string is in fact the ending of the string because as you say there is no difference between '\0' and '0'.

    So, as you say, that any elements of the array that you don't specify a value for are initialized to 0, as long as at least one element is initialized.

    But what about structure? Suppose we have:
    Code:
    struct Student {int rollno, age; float marks; string name}
    .

    I initialize:
    Code:
    Student = {20,18};
    In this case I have only initialized "rollno" and "age". The rest of the members would contains garbage values and would not be initialized to "0" as is the case with array. Correct?
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That would mean the beginning of the string is in fact the ending of the string because as you say there is no difference between '\0' and '0'.
    Grrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr. There is much much much much much much much difference between '\0' and '0' as we have told you many times. '\0' is the character 0. '0' is the character 48. It's kind of hard to tell whether you've ever figured out the difference if you can't even parrot it back to us properly.

    According to the C standard,
    Quote Originally Posted by C Standard, section 6.7.8, para 19
    [A]ll subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.
    Objects that have static storage duration (e.g., globals at the top of a file) are implicitly initialized to 0.

    (EDIT: I quoted the C standard, because ..., well I'm not sure why. The C++ standard has the language in 8.5.1, which is not word-for-word the same but also ends up with zero-initialization.)
    Last edited by tabstop; 06-21-2011 at 05:04 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir View Post
    1: Is this possible to have an array of zero size such as array[0]? What would it mean?
    No.
    Oh?
    Code:
    #include<stdio.h>
    int main( void )
    {
        int array[0];
        return 0;
    }
    While this is C and not C++, it compiles fine, and the only warning I get is that I haven't used 'array'. I would be surprised if the C++ had a special rule against a zero sized array.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by quzah View Post
    Oh?
    Code:
    #include<stdio.h>
    int main( void )
    {
        int array[0];
        return 0;
    }
    While this is C and not C++, it compiles fine, and the only warning I get is that I haven't used 'array'. I would be surprised if the C++ had a special rule against a zero sized array.


    Quzah.
    C++ code - 6 lines - codepad

    It does.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since I've got my standards with me:
    Quote Originally Posted by C++ standard, 8.3.4
    If the constant-expression (in the brackets--tabstop) is present, it shall be an integral constant expression and its value shall be greater than zero.
    And despite the above:
    Quote Originally Posted by C standard, section 6.7.5.2
    If the expression (in the brackets--tabstop) is a constant expression, it shall have a value greater than zero.
    On GCC, I have to throw in the -pedantic flag to get the warning
    Code:
    test.c:3: warning: ISO C forbids zero-size array `array'

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by quzah
    While this is C and not C++, it compiles fine, and the only warning I get is that I haven't used 'array'. I would be surprised if the C++ had a special rule against a zero sized array.
    Quote Originally Posted by tabstop
    On GCC, I have to throw in the -pedantic flag to get the warning
    gcc allows zero length arrays as a language extension.
    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. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    On GCC, I have to throw in the -pedantic flag to get the warning
    I usually compile that way. The one time I don't...


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two small queries
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2011, 04:30 AM
  2. special queries for databases
    By Leite33 in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2006, 05:23 AM
  3. DLL Making and Symbols - 2 Queries
    By Tronic in forum C++ Programming
    Replies: 9
    Last Post: 12-31-2004, 01:11 AM
  4. text file queries
    By ozzy34 in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2004, 10:42 AM
  5. Hash File Queries
    By 1999grandamse in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2002, 02:18 PM