# Thread: some queries on arrays

1. ## 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".

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

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.

4. Originally Posted by King Mir
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".

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?

5. 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,
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.)

6. Originally Posted by King Mir
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.

7. Originally Posted by quzah
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.

8. Since I've got my standards with me:
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:
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. 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.
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.

10. Originally Posted by tabstop
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.

Popular pages Recent additions