0

I have two functions. One that creates a multiplication table of a given number and the other function prints the array out. Below is my code:

Here's the error (Line 18):

expression must be a pointer to a complete object type

How do I fix this error and print the array? Also, I don't know how to print a new line after every row.

#include "multiplication.h"
#include <stdio.h> 
int arr[][];
void mulitpication(int num){
    /* initialize array and build*/
    int arr[num][num];
    for(int i=0; i<num;i++){
        for(int j=0;j<num;j++){
            arr[i][j]= (i+1)*(j+1);
        }
    }
}
    
void print(int arr[][]){
    /* print the arr*/
    int i;
    for(i=0;i<sizeof(arr);i++){
        for(int j=0;j<sizeof(arr);j++){
            printf("%d ",arr[i][j])**(line 18)**;
        }
            
    }
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
Justin
  • 67
  • 6
  • 1
    Post code as text in code blocks, not as images, so people can copy and paste into the answer. – Barmar Sep 16 '20 at 22:21
  • The `arr` variable is local to the `multiplication()` function. It can't be accessed from the `print()` function. – Barmar Sep 16 '20 at 22:22
  • You should declare the array in `main()`. Then you can pass it as a parameter to each function. – Barmar Sep 16 '20 at 22:22
  • You can't use `sizeof` to get the size of an array parameter. \ See https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array – Barmar Sep 16 '20 at 22:23
  • Ok, I deleted the pictures and posted the code. However, how am I suppose to add a new line after each row when printing. Ideally, if the number is 4, it should print {{1,2,3,4},{2,4,6,8},{3,6,9,12},{4,8,12,16}} as a multiplication table @Barmar – Justin Sep 16 '20 at 22:32
  • @Barmar thank you. I'm new to C so I'm still learning the ins and out. How to I add a new line after printing a row? – Justin Sep 17 '20 at 00:11
  • Don't you see the `printf("\n");` statement in my answer? – Barmar Sep 17 '20 at 00:12
  • I see it now, thank you. – Justin Sep 17 '20 at 00:59

2 Answers2

1

If using C99 or later with VLA support, pass into the print function the dimensions needed.

// void print(int arr[][]){
void print(size_t rows, size_t cols, int arr[row][col]){
    size_t r,c;
    for (size_t r = 0; r < rows; r++) {
      for (size_t c = 0; c < cols; c++) {
        printf("%d ",arr[r][c]);
      }
      printf("\n");
    }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Thanks @chux, By the way, what do you mean by C99? – Justin Sep 17 '20 at 00:52
  • @Justin [C99](https://en.wikipedia.org/wiki/C99) is the 1999 version of C that introduced [variable length arrays](https://en.wikipedia.org/wiki/Variable-length_array#C99). – chux - Reinstate Monica Sep 17 '20 at 02:27
  • @Justin [What is the difference between C, C99, ANSI C and GNU C?](https://stackoverflow.com/questions/17206568/what-is-the-difference-between-c-c99-ansi-c-and-gnu-c) – Lundin Sep 17 '20 at 07:58
0

You need to declare the array in main(), so it can be passed to both functions.

When an array is passed as a function parameter, it just passes a pointer. You need to pass the array dimensions, they can't be determined using sizeof.

To get each row of the table on a new line, put printf("\n"); after the loop that prints a row.

#include <stdio.h> 

void multiplication(int num, arr[num][num]){
    /* initialize array and build*/
    for(int i=0; i<num;i++){
        for(int j=0;j<num;j++){
            arr[i][j]= (i+1)*(j+1);
        }
    }
}
    
void print(int num, int arr[num][num]){
    /* print the arr*/
    int i;
    for(i=0;i<num;i++){
        for(int j=0;j<num;j++){
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    int size;
    printf("How big is the multiplication table? ");
    scanf("%d", &size);
    int arr[size][size];
    multiplication(size, arr);
    print(size, arr);
    
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks @Barmar, I really have learned quite a bit from you. If you don't mind me asking, what does sizeof return anyway? – Justin Sep 17 '20 at 00:54
  • It returns the size of its parameter. When an array decays to a pointer, it returns the size of the pointer (usually 4 or 8 bytes), not the size of the array it points to. – Barmar Sep 17 '20 at 01:02