Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:23:16+00:00 2026-06-05T03:23:16+00:00

I cannot understand why i is set to 0 right after array is initialized

  • 0

I cannot understand why i is set to 0 right after array is initialized to zero.

The program is working fine because I have reinitialized value of k to i.
But I could not find out why i becomes 0.
And why memset() is clearing the array, or setting the array to 0?

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
    long long int i = 123456789;
    long long int j = 987654321;
    long long int cnt = 0;
    int array[9] ;
    int xyz, k, x, rem, se;
    xyz = 0;

 //   printf("I = %llf", i);
    for (i; (i < j) && (cnt < 100000); i++)
    {
        k = i;
        x = 0;
        for (se = 0; se <= 9; se++)
        {
            array[se] = 0;
        }

/*************************************************/

        i = k;   // Here i becomes zero. Why?

/************************************************/

        //memset(array, 0, 9);   

        while(k != 0)
        {
            rem = k % 10;
            for(se = 0; se <= 9; se++)
            {
                if(rem == array[se])
                {
                    xyz = 1;
                    break;
                }

            }
            if(rem == array[se])
                {
                    xyz = 1;
                    break;
                }
            array[x++] = rem;
            k = k / 10;
        }
        if (xyz != 1)
        {
            cnt++;
        //    printf("Cnt = %d  ", cnt);
        //    printf("The value i is = %lld\n", i);
        //    Sleep(10);
        }
        xyz = 0;
        // printf("The value i is = %lld\n", i);
        // printf("Cnt = %d  \n", cnt);
        fflush(stdin);
    }
    printf("The value i is = %lld \n", i-1);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:23:17+00:00Added an answer on June 5, 2026 at 3:23 am

    You have a buffer overflow; since the buffer is on the stack, it might be regarded as a form of Stack Overflow†.

    int array[9];    // Elements array[0] .. array[8]
    ...
    
    for (se = 0; se <= 9; se++)
    {
        array[se] = 0;
    }
    

    It must be k that is being overwritten with the extra 0; i is assigned 0 because that’s the value in k. You invoke ‘undefined behaviour’ when you write outside the boundaries of an array, as you did here. Undefined behaviour means that anything can happen and it is OK. Sometimes, it will seem to work; sometimes, there’ll be unexpected side effects. Avoid ‘undefined behaviour’ at all costs.

    The idiomatic for loop is:

    for (se = 0; se < 9; se++)
        array[se] = 0;
    

    Note the < instead of <=.

    † There are those who would disagree. See the comments.


    You also ask about the (commented out) call to memset():

    //memset(array, 0, 9);
    

    The third parameter to memset() is the size in bytes of the area of memory to be set. You are setting 9 bytes out of a total of (probably) 36 in the array, which is unlikely to be what you wanted.

    Here, the array is defined in the same function so it is safe and sensible to write:

    memset(array, 0, sizeof(array));
    

    If array was a parameter passed to a function, that would not work correctly; you would need a different size. For example:

    void somefunc(int array[], int num)
    {
        ...
        memset(array, 0, num * sizeof(array[0]));
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot understand these 2 strange behaviour 1. First Behaviour I have declared a
I cannot understand the Java memory usage. I have an application which is executed
For reasons I cannot understand, the people who supply my tables do not have
I cannot understand why the date is never set in the title - it's
I cannot understand the error because I'm not trying to convert to numeric the
I am using Stripes for a project and have a situation I cannot understand.
I am learning Ruby and I have a bug I cannot understand. I have
I cannot understand the calculation 66 ⊕ fa = 9c. The sum is clearly
I cannot understand how Bing Community site is implemented. Clicking one of the All
I cannot understand why the code below is giving me this error in firebug

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.