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

  • SEARCH
  • Home
  • 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 8141051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:22:06+00:00 2026-06-06T12:22:06+00:00

I am using Code::Blocks 10.05, and the GNU GCC Compiler. Basically, I ran into

  • 0

I am using Code::Blocks 10.05, and the GNU GCC Compiler.

Basically, I ran into a really strange (and for me, inexplicable) issue that arises when trying to initialize an array outside it’s declared size. In words, it’s this:

*There is a declared array of size [x][y].

*There is another declared array with size [y-1].

The issue comes up when trying to put values into this second, size [y-1] array, outside of the [y-1] size. When this is attempted, the first array [x][y] will no longer maintain all of its values. I simply don’t understand why breaking (or attempting to break) one array would affect the contents of the other. Here is some sample code to see it happening (it is in the broken format. To see the issue vanish, simply change array2[4] to array2[5] (thus eliminating what I have pinpointed to be the problem).

#include <stdio.h>

int main(void)
{
    //Declare the array/indices
    char array[10][5];
    int array2[4]; //to see it work (and verify the issue), change 4 to 5
    int i, j;

    //Set up use of an input text file to fill the array
    FILE *ifp;
        ifp = fopen("input.txt", "r");

    //Fill the array
    for (i = 0; i <= 9; i++)
    {
        for (j = 0; j <= 5; j++)
        {
            fscanf(ifp, "%c", &array[i][j]);
            //printf("[%d][%d] = %c\n", i, j, array[i][j]);
        }
    }

    for (j = 4; j >= 0; j--)
    {
        for (i = 0; i <= 9; i++)
        {
            printf("[%d][%d] = %c\n", i, j, array[i][j]);
        }
        //PROBLEM LINE*************
        array2[j] = 5;

    }

    fclose(ifp);
    return 0;

}

So does anyone know how or why this happens?

  • 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-06T12:22:07+00:00Added an answer on June 6, 2026 at 12:22 pm

    Because when you write outside of an array bounds, C lets you. You’re just writing to somewhere else in the program.

    C is known as the lowest level high level language. To understand what “low level” means, remember that each of these variables you have created you can think of as living in physical memory. An array of integers of length 16 might occupy 64 bytes if integers are size 4. Perhaps they occupy bytes 100-163 (unlikely but I’m not going to make up realistic numbers, also these are usually better thought of in hexadecimal). What occupies byte 164? Maybe another variable in your program. What happens if you write to one past your array of 16 integers? well, it might write to that byte.

    C lets you do this. Why? If you can’t think of any answers, then maybe you should switch languages. I’m not being pedantic – if this doesn’t benefit you then you might want to program in a language in which it is a little harder for you to make weird mistakes like this. But reasons include:

    • It’s faster and smaller. Adding bounds checking takes time and space, so if you’re writing code for a microprocessor, or writing a JIT compiler, speed and size really do matter a lot.
    • If you want to understand machine architecture and go into hardware, e.g. if you’re a student, it’s a good gateway from programming into OS/hardware/electrical engineering. And much of computer science.
    • Being close to machine code, it’s standard in a way that many other languages and systems have to, or can easily, support some degree of compatibility with.
    • Other reasons that I would be able to give if I ever actually had to work this close to the machine code.

    The moral is: In C, be very careful. You must check your own array bounds. You must clean up your own memory. If you don’t, your program often won’t crash but will start just doing really weird things without telling you where or why.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Code::Blocks IDE with the GNU GCC compiler. struct test { char
I'm compiling this code with GNU GCC Compiler in Code Blocks but for some
I am using Code Block with GNU GCC Compiler. And I am trying this
I switched from Visual Studio to Code::Blocks yesterday, and just had some strange compiler
I'm compiling using Code::Blocks on Windows 7 using the MinGW compiler (which I can
I'm currently working on a program (in C++, using Code::Blocks) that uses a lot
I'm using Code::Blocks for a project that uses templated classes, and I'm separating the
I'm learning about strings in c. I am using code::blocks as a compiler, even
I can't get org-mode to fontify code blocks the way it should...I'm using Gnu
I am using code::blocks, with, I believe gcc. Given the example code (this is

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.