DEV Community

Discussion on: Introduction To Arrays

Collapse
 
pauljlucas profile image
Paul J. Lucas

Arrays in C are classified into one-dimensional, two-dimensional, and multi-dimensional arrays.

No. "Two" is "multi". "Multi" means more than one, not more than two. So there are only one- and multi-dimensional arrays. But even that's wrong. All arrays in C are one-dimensional. What you call "multi-dimensional" is really just a one-dimensional array of a type that just so happens to be another array. It's not a special case.

In static uninitialized arrays, all the elements initially contain garbage values ...

No. If an array is declared static, then, if not explicitly initialized, they're initialized to 0 (or equivalent); hence, there's no such thing as a "static uninitialized array." The same is true for any array (static or not) declared at file scope. Only non-static arrays local to a function or as a member of a struct are not initialized.

printf("%d", nums[-1]); //Garbage value will be printed

At best; or your program will crash.

You never mention that you can omit the array size:

int x[] = { 0, 1, 2 }; // compiler will deduce size of 3
Enter fullscreen mode Exit fullscreen mode

You never mention that you can copy arrays using memcpy(). You also never mention that arrays are copied implicitly if they're part of a struct:

struct S {
    int x[3];
};

struct S a, b;
// ...
a = b;          // b::x _is_ copied to a::x
Enter fullscreen mode Exit fullscreen mode

You mention multi-dimensional arrays, but never discuss them.
You don't cover the odd case of passing "arrays" as function parameters.

Collapse
 
abbhiishek profile image
Abhishek kushwaha

Thanks @pauljlucas for the correction ♥