[Solved] Why no error is thrown in compilation? [closed]


Can this be a way to initialize an array without declaring its size?

No.

All int *a; does, if defining a pointer to int, which, as not being initialised, points somewhere, “nowhere”, to “invalid” memory.

Any invocation of the []-operator on a, without beforehand having made a point to any valid memory (as for example the OP’s your code does) invokes undefined behaviour, anything may happen from then on, from crash to seemingly working.


Assuming that by “declare” you in fact mean “define” the only way to define an array with “unknown” size is by providing an initaliser (with well known size) at definition of the array, like so:

int arr[] = {-1, 0, 42}; /* Defines arr to be an array of three ints. */

Right after the creation/initialisation arr has a well know size of three int.

Please note that the = sign above is not the same as the assignment operator =. The following code will not compile:

int arr[]; 
arr = {-1, 0, 42}; /* Invalid, as an array may not be assigned. */

To make GCC be more chatty about what all might be suspicious in the code it is fed pass the following options when compiling:

-Wall -Wextra -pedantic -Wconversion

0

solved Why no error is thrown in compilation? [closed]