I created a dynamic array ,and i need to initialize all the members to 0.
How can this be done in C?
array = (int*) malloc(n*sizeof(int));
In this case you would use
array = (int*) calloc(n, sizeof(int));
It's safe to assume that all systems now have all zero bits as the representation for zero.
§18.104.22.168 guarantees this to work:
For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type.
It's also possible to do a combination of
memset(), but for reasons discussed in the comments of this answer, it is likely to be more efficient to use