Hi!
I'm a student learning C language. During my exercise I have faced a weird problem(atleast to me).
I have encountered two declarations:

int size;
int arr[size];

I'm unable to figure out the actual dimension of the array and its lenght as the variable size is only declared not defined; meaning it has not been assigned any value yet.

Another statement which maded me scratch my head was:

int siy[2+3]

What is the dimension and length of this array?

I'll be very thankful, if you'll help me........

I'm unable to figure out the actual dimension of the array and its lenght as the variable size is only declared not defined; meaning it has not been assigned any value yet.

It still has a value; just not one assigned by you. The int named "size" occupies some memory, and that memory has numbers in it. They could be anything. Anything at all. So you've got an array of unknown size. This is a very bad idea.

What is the dimension and length of this array?

It's a one-dimensional array of 5 ints.

Your first piece of code is illegal.

In C89 it's illegal because array sizes need to be compile-time constants, so variables are not allowed. A conforming compile should give an error message or at the very least a warning when compiling in C89 mode.

In C99 it's allowed to use variables as array sizes, but it's still illegal because size is uninitialized and thus using it invokes undefined behavior.

As a(n irrelevant) side note: it is defined, just not initialized. To declare a variable without defining it, you'd use the extern keyword.

---

Your second piece of code defines a one-dimensional array of size 5 because 2+3 is 5. A multi-dimensional array would be defined by using multiple pairs of brackets (like int siy[2][3]).

    int size;
    int arr[size];

Yes, size is uninitialized, and the behaviour is undefined. This code is not correct. Also keep in mind that this will only work with C99 and later (C89 does not support variable length arrays).

For fun, you can try to find the length of the array with sizeof(arr) / sizeof(arr[0]); (This does not work for dynamically allocated arrays by the way.)

What is the dimension and length of this array?

In C, arrays are one-dimentional. You can either give them more dimentions by "nesting" them, or using arithmitic to index a one-dimentional array as a multi-dimentional array.

The length is 5, and the number of dimentions is 1 in your example.

If you're working with a dynamically allocated array, you need to "remeber" what the size and dimentions are.

If you're working with a statically allocated one-dimentional array, you can get the size of it with the code from above. You won't be able to find the number of dimentions unless you "remember" it.

It still has a value

That's not necessarily true:

An uninitialized local variable may contain a trap representation for its type (and thus not a valid value), in which case reading it will invoke undefined behavior (possibly a crash).

Furthermore reading an uninitialized local variable whose address is never taken invokes undefined behavior regardless of whether or not the given type has a trap representation on your implementation.

Thank you for all your help....
sepp2k you explained to me greatly...
Thanks alot...!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.