Click here to Skip to main content
15,499,430 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
int b[10]={0};
for (int i=0;i<=10;i++)

What I have tried:

Can't understand the question.
Updated 14-May-21 4:17am
Rick York 14-May-21 12:04pm    
What question do you not understand?
M A.h 2021 15-May-21 2:16am    
No. There is no problem and understood.

The code initializes each of the 10 cells in the array b to 0. It then uses a for loop to replace each of those cells with a 1.

The 10 cells are addressed as b[0]...b[9]. However, the index used to address the array elements is allowed to reach 10, and b[10] does not exist. If the environment in which the program is running includes safety checks, the program will terminate abnormally (also referred to as dumping, aborting, coring, trapping, or getting killed). If there's no error checking, the program will corrupt the memory location associated with b[10], which could be the location of another variable. In a non-trivial program, this usually results in mysteriously incorrect behavior that can very hard to debug.
Share this answer
Can't understand the question.

Translation of question: What is doing this piece of code.
Do you have a problem to understand 1 of those lines of code ?
Share this answer
M A.h 2021 14-May-21 7:46am    
Is a practice and I don't know exactly what the question wants?
M A.h 2021 14-May-21 7:48am    
What is this code error?
Patrice T 14-May-21 8:57am    
As far as I can see, there is no error.
Share this answer
"It is an array (ugly) initialization".
It is a wrong (ugly) array initialization".
See, for instance Arrays - C++ Tutorials[^]
Share this answer
Greg Utas 14-May-21 9:25am    
I don't think it's wrong. {0} fills the entire array with zeroes.
CPallini 14-May-21 12:08pm    
The for loop is wrong.
Greg Utas 14-May-21 12:09pm    
Sorry, I thought you were referring to the {0}. Yes, the for loop is wrong.
M A.h 2021 14-May-21 13:15pm     CRLF
Is the for loop written like this? for (int i=0;i<=9 ;i++)
jeron1 14-May-21 13:55pm     CRLF
Many times it written like, for (int i = 0; i < 10; i++) // use the size of the array instead of size-1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900