1

I'm trying to print a multiplication table that is stored in a 2D array by using pointers to pointers. When I run this code, it only prints out the "1s" column. If I remove the outer loop's incrementation and the line break conditional, then it will print the numbers 1-10 ten times. I don't understand this behavior.

printf("\n");
printf("Multiplication Table in standard array syntax\n");

int a[10][10];
int j;
int k;
int prod;
for (j = 1; j < 11; j++){
    for (k = 1; k < 11; k++){
        prod = j*k;
        a[j - 1][k - 1] = prod;
        printf("%d ", a[j - 1][k - 1]);
        if (k == 10)
            printf("\n");
    }
}

printf("\n");
printf("Multiplication Table in standard + pointer syntax\n");

int *pi[10];
int x;
for (x = 0; x <10; x++){
    pi[x] = a[x];
}

int y;
for (x = 0; x < 10; x++){
    for (y = 0; y < 10; y++){
        printf("%d ", *(pi[x] + y));
        if (y == 9)
            printf("\n");
    }
}

printf("\n");
printf("Multpilication Table in pure pointer syntax\n");

int **ppi;
ppi = pi;
int p;
int q;
for (p = 0; p < 10; p++){
    for (q = 0; q < 10; q++){
        printf("%d", *(*ppi + q));
        if (q = 9)
            printf("\n");
    }
    ppi++;
}

}
1
  • @Gone What is about that code? Commented Feb 14, 2014 at 17:54

3 Answers 3

2

The only typo is if (q = 9)

Change it to if (q == 9) as that is terminating your loop

Sign up to request clarification or add additional context in comments.

5 Comments

there is not mistake?
In which question? What line?
Thanks, I know that's how I'm supposed to set it up, but I make that mistake so often :P
:) Weekend has something to do with it i guess ;)
@Gone :D No I am not drunk :P
1

the first 2 blocks are working fine, in the third block you have q = 9 instead of q == 9 (as gregkow said). There is a coding style for if-clauses (called Yoda Conditions) thas has the value first:

if( 9 == q )

This would fail on a typo like this:

if( 9 = q )
main.c: In function 'main':
main.c:51:15: error: lvalue required as left operand of assignment
     if (9 = q)
           ^

Comments

0

I would suspect the if (q = 9) line. C has no problem with this (it sets q equal to 9 and then checks if nonzero). This would likely both end your for loop early and print new lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.