arr[][n]={0};
arr[10][10]={0};
they both are different…when we output the array…
first one gives garbage values at some points
second one gives all zeros
could anyone please tell why this is happening
arr[][n]={0};
arr[10][10]={0};
they both are different…when we output the array…
first one gives garbage values at some points
second one gives all zeros
could anyone please tell why this is happening
here rows are not specified, while initializing, either put {{0},{0}…} zeroes this way row times, else it will give you garbage value as values were never initialized due to incorrect initialization.
here size is correctly defined, so all elements from 0,0 to 9,9 will be zero!
similarly if their is an array arr[n][n]={0}
here i specified both rows and cols but it also gives the garbage value even if i specified the rows…why??
@manthan.joshi.jiit yes this is a flaw of dynamic memory allocation,so either static arrays(declared with proper integer like arr[17][6]) are used, or memset is used for initialization.
Could you elaborate it a bit please
arr[n][n] is an example of dynamic memory allocation (during compile time), to initialize all elements you can use memset function.
you can read more about memset here http://www.cplusplus.com/reference/cstring/memset/?kw=memset