Arrays!
-
Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.
-
Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.
If you don't define all elements of the array then they will be undetermined and can be anything. It will be pure chance that they are 0. If you want the array elements to default to NULL you need to NULL the array first. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain) -
Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.
First of all, you cannot trust on compiler about filling all assigned memory with 0's. In VC++, it is initialized with garbage, so be sure to call a function to initialize array, for example, ZeroMemory or memset. Now to your question. If you declare char array[] you have an array of bytes, so you have to compare against 0, not '0' (comparing with '0' is like comparing with 0x30) Jaime
-
Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.
-
Also note that NULL and 0, '0' are NOT the same. 0 = a value NULL is nothing, nada, ... "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
-
Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.
-
First of all, you cannot trust on compiler about filling all assigned memory with 0's. In VC++, it is initialized with garbage, so be sure to call a function to initialize array, for example, ZeroMemory or memset. Now to your question. If you declare char array[] you have an array of bytes, so you have to compare against 0, not '0' (comparing with '0' is like comparing with 0x30) Jaime
Well. When an array is declared, its content is undefined unless you give it an initial value. Initial values are assigned to an array by listing values withing {}. If your array is larger than your initialization list, the remaining elements will be assigned 0 by the compiler.
int a[4]; // Undefined value int b[4] = {1,2}; // Contains values 1, 2, 0, 0
/Per -
Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.
Also, not mentioned before, is that static arrays are always initialized with zero for each value. I wouldn't recommend making a variable static for that reason alone, but it is interesting to remember that for future reference. If you have an array that is in a global function and you don't want it to be reallocated on the stack every time the function is called, you could make an array static. The values are all zero the first time the function executes and after that, the values will stay in memory. The initialization only takes place once, even though the function call could be made many times.