-3

I have this array :

array[0][0] = 1; 
array[0][1] = 2;
array[0][2] = 3;
array[0][3] = 4;

How could I do something like this ?

int a = 0;
int b = 1;

printf("%d",array[a][b]);

C89 is a requirement. Here is a MCVE:

int main(int argc, char *argv[])
{
    int array[0][4] = {1, 2, 3, 4}, i = 0;

    for (i = 0 ; i < 4 ; i++)
    {
        printf("%d\n", array[0][i]);
    }

    return 0;
}

Output:

1512079328
32764
0
3
Lundin
  • 195,001
  • 40
  • 254
  • 396
shellwhale
  • 820
  • 1
  • 10
  • 34
  • 5
    What's the problem? your code seems correct. – David Ranieri Nov 16 '18 at 13:38
  • In 2018 consider using [C11](https://en.wikipedia.org/wiki/C11_(C_standard_revision)) (so read [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)...) or at least [C99](https://en.wikipedia.org/wiki/C99). C89 is *really old* and obsolete. Consider upgrading your compiler – Basile Starynkevitch Nov 16 '18 at 13:40
  • 1
    `int array[0][3]` is "logically" same as `int array[3]`. You don't need two dimensional array – Swanand Nov 16 '18 at 14:08
  • Also, C99 and C89 are very close. What you learn for C99 is very often correct for C89 – Basile Starynkevitch Nov 16 '18 at 14:12
  • 1
    Your current example won't even compile since arrays of size `0` are not allowed. So your `Give me` cannot result from the code you shown. – Swordfish Nov 16 '18 at 14:12
  • Compile your code with all warnings and debug info: `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/) (and you might add `-std=c89` to the list of options to `gcc`). Improve it to get no warnings. Read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Basile Starynkevitch Nov 16 '18 at 14:17
  • I cleaned up the question a bit, hope you don't mind. It is fine now after the added example and should not be closed. – Lundin Nov 16 '18 at 14:44

2 Answers2

4

Your array needs at least 1 in the first dimension and 4 in the 2nd.

int array[1][4];
//        ^  ^

Reason: Arrays in C and C++ have valid indexes from 0 up to size - 1. So if you want to access indexes 0 ... 3 the size of the array has to be at least 4.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
1

The cause of the problem is likely gcc "gnu mode" (gnu89) which is the default compiler setting on older compilers. It will not compile your code according to the C standard, but according to a non-standard invented by a gnu. Therefore your severe bugs passed.

If you instead compile the code as C language, using -std=c89 -pedantic-errors, you get the following list of diagnostic messages:

error: ISO C forbids zero-size array 'array' [-Wpedantic]|    
warning: missing braces around initializer [-Wmissing-braces]|    
warning: (near initialization for 'array[0]') [-Wmissing-braces]|   
error: excess elements in array initializer|   
error: (near initialization for 'array')|  

The cause is as already mentioned, that array size during declaration is not the same as array indexing during access. When fixed & compiled as C language, it compiles cleanly and runs correctly:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int array[1][4] = { {1, 2, 3, 4} };
    int i;

    for (i = 0 ; i < 4 ; i++)
    {
        printf("%d\n", array[0][i]);
    }

    return 0;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396