-4

So I used a gloabal variable called sample and a structure containing a char.Here is the declaration snippet

So later down the program what happens is when I assign a value to sample it overlaps with d[2].a[0]...before assigning after assigning

What do I do to fix this? Am I doing something wrong?

Minimal rep:

#include<stdio.h>
#include<stdlib.h>

char sample;
int j=0,level=0,c=0;
struct grid
{
    char a[11];
}d[2];

void set(int level)  //used to initialize the array
{
    j=0;
        for(j=0;j<11;j++)
            if(j!=3&&j!=7)
                d[level].a[j]=' ';

    d[level].a[3]='|';
    d[level].a[7]='|';
    d[level].a[11]=NULL;

}
int main()
{
    set(level++);
    set(level++);
    set(level);
    sample='x';
    printf("%c",d[2].a[0]);
}

It should print space but instead it prints x

dbush
  • 205,898
  • 23
  • 218
  • 273
G-360
  • 1
  • 1
  • 2
    Please read this: [ask]. Then [edit] your question and add your code as _text_ and as [mcve]. https://idownvotedbecau.se/imageofcode – Jabberwocky Feb 26 '20 at 10:05
  • Very wild guess: check if none of your array indexes goes out of bounds. – Jabberwocky Feb 26 '20 at 10:07
  • @jabberwocky well the array assignment worked fine actually the problem only happened after i assigned a value to sample – G-360 Feb 26 '20 at 10:12
  • As soon as you access an array with an out of bounds index you enter the realm of undefined behaviour. A typical thing that happens if you write into an array with an out of bounds index is overwriting another unrelated variable. – Jabberwocky Feb 26 '20 at 10:14
  • Read [this](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – Jabberwocky Feb 26 '20 at 10:16
  • @Jabberwocky i just checked, the indices were 1 and 5 as..d[1].a[5], i declared as a[11] with d[2] so it was in bounds so it probabily wasnt the case... should i try using malloc instead?? or does it have something to do with sample (the variable ) being a char ?? – G-360 Feb 26 '20 at 10:21
  • 1
    Maybe, but your pictures of code are basically useless. You need to [edit] your question and provide [mcve] along with a simple example of input that produces the problem. Also provide the actual and the expected output. – Jabberwocky Feb 26 '20 at 11:02
  • @Jabberwocky i understand and am finally able to recreate the problem but now its closed and i cant ask any more questions because it wasnt recieved well whatdoido now? – G-360 Feb 26 '20 at 11:09
  • Please take 1 or 2 minutes and lear how to format code on stackoverflow. It's really not difficult. – Jabberwocky Feb 26 '20 at 11:09
  • Your compiler should have warned you: `d[level].a[11] = NULL;` -> `d[level].a[11] = 0;`, bit I'm not sure this is the problem. Sorry the question is closed now. I voted to reopen it, but crappy questions tend to be closed quickly. – Jabberwocky Feb 26 '20 at 11:12
  • @jabberwocky i just tried it , didnt fix it . well thanks for your time, ill try to find more instances of this and then repost 4 days later – G-360 Feb 26 '20 at 11:15
  • `printf(">%c<",d[2].a[0]);`-> `printf(">%c<",d[1].a[0]);`. `d[2]` is out of bounds. Remember: the first index is 0, not 1. – Jabberwocky Feb 26 '20 at 11:18
  • Thanks that worked i changed declaration to d[3]. was d[2] out of bounds because it was the last one? i dont seem to fully understand this could you send me somewhere that explains this – G-360 Feb 26 '20 at 11:23
  • It's [undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior). Read [this](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) (as already mentioned in a previous comment). – Jabberwocky Feb 26 '20 at 11:31
  • `char a[11]` implies `a` ranges from index `0` to `10`, index `11` is out of bounds. Same with `d[2]`. – Havenard Mar 10 '20 at 15:50

1 Answers1

0

The array d is defined with size 2, and those elements have indexes 0 and 1. The third call to set passes the value 2 to the function. This means the function tries to write to index 2 of the array which is past the bounds of the array. You also attempt to read from d[2] in main. Reading/writing past the end of an array invokes undefined behavior.

If you want the array to have 3 elements, declare it as such.

struct grid
{
    char a[11];
} d[3];
dbush
  • 205,898
  • 23
  • 218
  • 273