please what is wrong with this for loop

Discussion in 'C Programming' started by nnaemeka.david@gmail.com, Sep 22, 2012.

  1. Guest

    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;
    }
    , Sep 22, 2012
    #1
    1. Advertising

  2. 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*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);


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

    > }
    > return 0;
    > }


    --
    Ben.
    Ben Bacarisse, Sep 22, 2012
    #2
    1. Advertising

  3. Guest

    On Saturday, September 22, 2012 5:36:38 PM UTC+1, Ben Bacarisse wrote:
    > 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*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);

    >
    >
    >
    > 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
    , Sep 23, 2012
    #3
  4. Geoff Guest

    On Sun, 23 Sep 2012 10:21:56 -0700 (PDT),
    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.
    Geoff, Sep 23, 2012
    #4
  5. Joe Pfeiffer Guest

    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.
    Joe Pfeiffer, Sep 23, 2012
    #5
  6. Angel Guest

    On 2012-09-22, <> 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*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.

    > }
    > 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
    Angel, Sep 24, 2012
    #6
  7. Kaz Kylheku Guest

    On 2012-09-22, <> 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.
    Kaz Kylheku, Sep 24, 2012
    #7
  8. 在 2012å¹´9月23日星期日UTC+8上åˆ12æ—¶18分46秒,写é“:
    > 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"
    涵曦, Oct 18, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    501
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    545
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,739
    Mike Hewson
    Jan 7, 2005
  4. mera
    Replies:
    7
    Views:
    1,095
  5. Isaac Won
    Replies:
    9
    Views:
    363
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page