Newbie
Newbie

Reputation: 31

2d C array isn't printing or is creating incorrectly

I wrote a piece of c code to create a 2d array and then print it. However it doesn't print and I can see that each loop seems to be running for the correct time.

I'm very new to c so this answer might be really basic sorry.

my code compiles fine (gcc) but then doesn't return what I expect. im using ubuntu 18.04 in WSL with gcc as my compiler.

heres my code:


#include <stdio.h>

int main(void) {
    int i, k;

    int array[2][3] = {0};

    for (i = 0; i < 2; i++) {
        printf("l1 ");
        for (k = 0; k < 3; k++) {
            scanf("%d", &array[i][k]);
            printf("l2 ");
        }
    }

    for (int i = 0; i < 3; ++i) {
        printf("l3 ");
        printf("%d ",  array[k][i]);

        if(i == 2) {
                i = -1;
                ++k;
                printf("\n");
        }
        if(k == 3) {
                printf("yay");
                return 0;
        }
    }
}

the terminal window looks like this:

l1 1
l2 2
l2 3
l2 l1 4
l2 5
l2 6
l2 l3 32602 yay

the numbers are the elements im entering apart from the final one which seems to be coming out as the output however it is nothing like what i would expect it to be.

i would expect it to print all my array elements but i dont understand why it doesnt since i dont get an error in gcc so my syntax seems fine.

Thanks.

Upvotes: 0

Views: 97

Answers (2)

Lidbey
Lidbey

Reputation: 371

    int i, k;

    int array[2][3] = {0};

    for (i = 0; i < 2; i++) {
        printf("l1 ");
        for (k = 0; k < 3; k++) {
            scanf("%d", &array[i][k]);
            printf("l2 ");
        }
    }

Consider this loop, it will leave 'k' at k=3 after finish, and then if you do this:

    for (int i = 0; i < 3; ++i) {
        printf("l3 ");
        printf("%d ",  array[k][i]); // exactly here

        if(i == 2) {
                i = -1;
                ++k;
                printf("\n");
        }
        if(k == 3) {
                printf("yay");
                return 0;
        }
    }

You will try to access arr[3][0] which is out of bounds, that's why 32602 is printed, And as you progress further with k=3 and(still) i=0, you get to if(k==3) condition, which prints yay, returns 0 and ends the process.

Upvotes: 1

Chan Kim
Chan Kim

Reputation: 5989

I recommend you to install ddd (a display debugger, internally it uses gdb by default). do

sudo apt install ddd

Now, do

gcc -g test.c

now you get a.out. (if you do gcc -g test.c -o test, you'll get test as executable)

and run the debugger.

ddd a.out

now you'll see the source code window and a command window. In the command window, type b main (break at main). The a.out runs and stops at main. (you know there stuffs running before main.) press 'n' for next (run till next line), to enter into a function press s (step-in), to finish a function from inside, press f, to run until line 100, u 100, etc. Search about gdb commands. To display a variable in a separate graphic window, do graph disp i. To set a break at line 100, b 100, to set break in line 100 if j == 3, b 100 if j == 3. To print a value i, p i, There is watch command (you run it, when the watched value changes, it stops). etc.
I don't know what you are trying to do, but didn't you forget settkgin k=0 between the two big for loops? Anyway, you could do debugging and find where it's wrong. ADD : If you would like to use plain gdb, after entering type layout src and you'll the source code. The remaning commands are the same except graphic display.

Upvotes: 1

Related Questions