please what is wrong with this for loop

N

nnaemeka.david

I wrote this program with a for loop that is misbehaving.
why does the for loop in main not output correctly when i use
a variable for the conditional step. if i change loopsie in the conditions step to the number 10, it runs correctly.

void init_array(int a[], int count){
for(int i=0; i<count; i++){
a=i*10;
}

}
int main(void) {
int my_array[5];
int loopsie=10, i;
init_array(my_array,loopsie);
for(i=0; i<loopsie; i++){
printf("element %d is %d\n", i, my_array);
}
return 0;
}
 
B

Ben Bacarisse

I wrote this program with a for loop that is misbehaving. why does
the for loop in main not output correctly when i use a variable for
the conditional step. if i change loopsie in the conditions step to
the number 10, it runs correctly.

void init_array(int a[], int count){
for(int i=0; i<count; i++){
a=i*10;
}

}
int main(void) {
int my_array[5];


Note the size: 5 elements.
int loopsie=10, i;
init_array(my_array,loopsie);

This call sets 10 elements. All bets are now off. Goodness knows
what's happened (most likely some local variables have unexpected
values) but this must be corrected before you can look any further.
for(i=0; i<loopsie; i++){
printf("element %d is %d\n", i, my_array);

}
return 0;
}
 
N

nnaemeka.david

(e-mail address removed) writes:


I wrote this program with a for loop that is misbehaving. why does
the for loop in main not output correctly when i use a variable for
the conditional step. if i change loopsie in the conditions step to
the number 10, it runs correctly.
void init_array(int a[], int count){
for(int i=0; i<count; i++){


int main(void) {

int my_array[5];



Note the size: 5 elements.


int loopsie=10, i;
init_array(my_array,loopsie);



This call sets 10 elements. All bets are now off. Goodness knows

what's happened (most likely some local variables have unexpected

values) but this must be corrected before you can look any further.


for(i=0; i<loopsie; i++){
printf("element %d is %d\n", i, my_array);




There's no declaration of printf. Do you #include <stdio.h> in the real

program?


return 0;

I did not think the size would be a problem? does it really matter? i thought my_array[5] to my_array[9] would print gibberish since they were non-initialized. what do you say to that? I believe that should be the supposed behaviour.
yes, i did include the stdio.h header .
thanks
 
G

Geoff

I did not think the size would be a problem? does it really matter? i thought my_array[5] to my_array[9] would print gibberish since they were non-initialized. what do you say to that? I believe that should be the supposed behaviour.

It very much DOES matter. You told the compiler the size of my_array
was five. You then fill it beyond the five places with ten values in
the init_array function. It is not uninitialized, you have initialized
it. You have also done it incorrectly.
 
J

Joe Pfeiffer

I did not think the size would be a problem? does it really matter? i thought my_array[5] to my_array[9] would print gibberish since they were non-initialized. what do you say to that? I believe that should be the supposed behaviour.
yes, i did include the stdio.h header .
thanks

Here's the relevant part of your code again:

Of course the size is important: it determines how many elements the
array has. As Ben pointed out, when you write to elements 5 through 9,
you're writing *somewhere*, and you have no idea what you're writing
over. I don't know where you get the idea that those elements are
uninitialized; in the code you originally posted you pass the number of
elements to initialize, and you passed a variable whose value was 10.
 
A

Angel

I wrote this program with a for loop that is misbehaving.
why does the for loop in main not output correctly when i use
a variable for the conditional step. if i change loopsie in
the conditions step to the number 10, it runs correctly.

void init_array(int a[], int count){
for(int i=0; i<count; i++){
a=i*10;
}

}
int main(void) {
int my_array[5];


Your array has five elements.
int loopsie=10, i;
init_array(my_array,loopsie);

You attempt to initialize ten elements, which means you are writing
beyond the end of your array into $deity knows what. From the structure
of your program, a possible result is that you clobber the loopsie and i
variables, but the whole thing is undefined and could do anything.
for(i=0; i<loopsie; i++){
printf("element %d is %d\n", i, my_array);


Since the loopsie variable is likely clobbered, there is no telling what
this loop will do now. There's a good chance it will attempt to read
beyond your program's allocated memory and trigger an exception from
your OS. On Unix and Linux, you'll most likely get a segmentation
fault if you didn't already get one before. But again, the whole thing
is undefined, it could do anything.
 
K

Kaz Kylheku

I wrote this program with a for loop that is misbehaving.

Overall a nice troll, but the style of stupidity you have chosen to
portray is a little too comically unrealistic.
 
Æ

涵曦

在 2012å¹´9月23日星期日UTC+8上åˆ12æ—¶18分46秒,[email protected]写é“:
I wrote this program with a for loop that is misbehaving.

why does the for loop in main not output correctly when i use

a variable for the conditional step. if i change loopsie in the conditions step to the number 10, it runs correctly.



void init_array(int a[], int count){

for(int i=0; i<count; i++){

a=i*10;

}



}

int main(void) {

int my_array[5];

int loopsie=10, i;

init_array(my_array,loopsie);

for(i=0; i<loopsie; i++){

printf("element %d is %d\n", i, my_array);

}

return 0;

}


I try to compile and run this, but the loopsie is out of the my_array's size and the compile don't tell us. so you could put right loopsie's value will safe for your program, like this

int loopsie=sizeof(my_array)/sizeof(int), i;

with this, you program will don't out of size.


"I'm very sorry, my english is very poor, I hope you can receive it"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top