-3

hello so I don't understand where is my error, I want to print the contents of a 2D matrix line by line:

void print_matrix(char s[], double m[], size_t rows, size_t cols)
{
printf("%s =\n", s);
    for (size_t i = 0; i < rows; i += 1)
    {
        for (size_t j = 0; j < cols; j += 1)
         {
            printf("%d ", m[i][j]);
         }
         printf("\n");
    }
}

I want it to be like this :
m1 =
1   2   3   4
5   6   7   8
9  10  11  12

it gives me an error here: m[i][j] but nothing else. Thanks for helping I'm a beginner

  • 1
    I would suggest to get a good book or tutorial and read some basics of arrays. – haccks Nov 15 '18 at 21:34
  • 3
    `double m[]` - doesn't look like 2D matrix. – Eugene Sh. Nov 15 '18 at 21:35
  • I know python and C# I have always done it like this and it worked – whohasfriends2 Nov 15 '18 at 21:35
  • `%d` will be expecting an int – lostbard Nov 15 '18 at 21:36
  • 4
    C is neither Python nor C#.. – Eugene Sh. Nov 15 '18 at 21:36
  • how is the data being stored ? – lostbard Nov 15 '18 at 21:37
  • Please give me an indication and not criticise me – whohasfriends2 Nov 15 '18 at 21:37
  • How is the 2D matrix created? And see [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). That's probably very relevant. – Andrew Henle Nov 15 '18 at 21:38
  • so what shall I print instead ? a float ? – whohasfriends2 Nov 15 '18 at 21:39
  • you should print a double (or "long float") with the `%lf` format specifier. – Christian Gibbons Nov 15 '18 at 21:41
  • I changed the %d to %lf but it gives me an error located at m[i][j] saying : error ^ – whohasfriends2 Nov 15 '18 at 21:42
  • ok i found out something. m[i][j] = m[i * columns + j] so thats what I did and used %f but now it prints me 0.000 and not the decimal I want (1) – whohasfriends2 Nov 15 '18 at 21:47
  • *it gives me an error located at m[i][j]* What error? You need to include that, too, along with the code that creates the matrix. What you're probably seeing is an error from trying to dereference `m` twice (two sets of `[]`). Since you passed only `double m[]`, the function sees `m` as a **one**-dimensional array, hence the likely error. – Andrew Henle Nov 15 '18 at 21:48
  • *i found out something. `m[i][j] = m[i * columns + j]`* That could still be wrong, depending on how the array was created. – Andrew Henle Nov 15 '18 at 21:50
  • OK EVERYONE I SOLVED IT the problem I got now is that it prints me 3.000 and I only want 3 how can I do this since I can only print %f if I print %d it fails – whohasfriends2 Nov 15 '18 at 21:50
  • 2
    You should ask a new question for a different problem. But before doing that, I recommend you look into the documentation for `printf` and read all about the format specifiers. – Christian Gibbons Nov 15 '18 at 22:01
  • @PilouPili -- it should _not_ be `double **m`. A pointer-to-pointer is not a 2d array, and passing an actual 2d array when a `double **` is expected by a function is a constraint violation leading to undefined behavior. A 2d array decays to `double (*)[]` in a function call (and in most expressions), and that is what the function needs to expect. Note that an array type like `double m[rows][cols]` is _adjusted_ to `double (*m)[cols]` in the function declaration at compile time. – ad absurdum Nov 16 '18 at 14:42

1 Answers1

1

If you want to be able to have m exist as a two-dimensional array of variable size dimensions, you can do something like this with VLAs. Since you are declaring variables inside of the conditions of for-loops, your compiler should be C99-compliant, so you should have access to VLAs.*

void print_matrix(char s[], size_t rows, size_t cols, double m[rows][cols])
{
    printf("%s =\n", s);
    for (size_t i = 0; i < rows; i += 1)
    {
        for (size_t j = 0; j < cols; j += 1)
         {
            printf("%lf ", m[i][j]);
         }
         printf("\n");
    }
}

*footnote: While VLAs are a mandatory inclusion in the C99 standard, the C11 standard stepped back and made them an optional feature.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29