2

I'm new to programming and a bit confused with arrays, what seems to be wrong in this code, as eclipse output console is saying ** Build of configuration Debug for project Project **

Internal Builder is used for build ** gcc -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.c gcc -oProject.exe main.o C:...\Documents\eclipse\mingw\bin..\lib\gcc\mingw32\3.4.5........\mingw32\bin\ld.exe: cannot open output file Project.exe: Permission denied collect2: ld returned 1 exit status Build error occurred, build is stopped Time consumed: 472 ms.

I will really appreciate your help...

int main()
{
    int box[2][2], rows, cols, x = 1;

    for (rows=0; rows < 2; rows++)
    {

        for (cols=0; cols < 2; cols++)
        {
            box[rows][cols] = x++;
            printf("%d", box[rows][cols]);
        }

    }
    fflush(stdout);
    getch();
    return 0;
}
  • works perfectly fine in gcc if you remove getch();. which environment are you working and which compiler are you using ? – Santhosh Pai Jun 11 '13 at 15:04
  • Refer This thread . – Santhosh Pai Jun 11 '13 at 15:10
  • 1
    I'm Using Eclipse... but i'm switching to Pelles C... might look gcc too thanks. – user2474851 Jun 11 '13 at 15:12
  • it's working fine in Pelles C. YAY! :) I have a question confused with arrays, the statement box[rows][cols] = x++; says increment x right? but x=1 already, so the value of box[rows][cols] or box[0][0] = 2 ? why in the print out it says 1? – user2474851 Jun 11 '13 at 15:20

1 Answers1

1

x++ is post increment so the value of x is used and then incremented and hence box[0][0] is 1

Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49
  • i'm sorry... in what statement does X++ do the increment, from what i understand box[rows][cols] = x++ is not equal to box[rows][cols] = 2, because you said box[rows][cols] = 1; does it mean will it run the whole statement in the inner for loop once and then after printf("%d", box[rows][cols]) that's the only time x increments to 2? – user2474851 Jun 11 '13 at 15:33
  • read this link to know about the increment and decrement operators http://cprogrammingcodes.blogspot.in/2011/09/increment-and-decrement-operators.html – Santhosh Pai Jun 11 '13 at 16:53