Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   please what is wrong with this for loop (http://www.velocityreviews.com/forums/t952534-please-what-is-wrong-with-this-for-loop.html)

nnaemeka.david@gmail.com 09-22-2012 04:18 PM

please what is wrong with this for loop
 
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]=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[i]);
}
return 0;
}


Ben Bacarisse 09-22-2012 04:36 PM

Re: please what is wrong with this for loop
 
nnaemeka.david@gmail.com 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++){
> a[i]=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[i]);


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

> }
> return 0;
> }


--
Ben.

nnaemeka.david@gmail.com 09-23-2012 05:21 PM

Re: please what is wrong with this for loop
 
On Saturday, September 22, 2012 5:36:38 PM UTC+1, Ben Bacarisse wrote:
> nnaemekaxdavid@gmail.com 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++){

>
> > a[i]=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[i]);

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

>
> > return 0;

>
> > }

>
>
>
> --
>
> Ben.

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

Geoff 09-23-2012 06:02 PM

Re: please what is wrong with this for loop
 
On Sun, 23 Sep 2012 10:21:56 -0700 (PDT), nnaemeka.david@gmail.com
wrote:

>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.

Joe Pfeiffer 09-23-2012 06:14 PM

Re: please what is wrong with this for loop
 
nnaemeka.david@gmail.com writes:
> 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:

>>
>> > int loopsie=10, i;

>>
>> > init_array(my_array,loopsie);


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.

Angel 09-24-2012 08:33 AM

Re: please what is wrong with this for loop
 
On 2012-09-22, nnaemeka.david@gmail.com <nnaemeka.david@gmail.com> wrote:
> 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]=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[i]);


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.

> }
> return 0;
> }
>



--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

Kaz Kylheku 09-24-2012 08:49 AM

Re: please what is wrong with this for loop
 
On 2012-09-22, nnaemeka.david@gmail.com <nnaemeka.david@gmail.com> wrote:
> 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.

涵曦 10-18-2012 01:07 AM

Re: please what is wrong with this for loop
 
在 2012年9月23日星期日UTC+8上午12时18分46 ,nnaemek...@gmail.com写道:
> 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]=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[i]);
>
> }
>
> 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"


All times are GMT. The time now is 07:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.