structure

please, why is this not working?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
#define MAX 9


typedef struct {
    int matA[N][N];
    int matB[N][N];
    int matSoucet[N][N];
    int matTranspA[N][N];
} typeMat;

void newMat(int mat[N][N])
{
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++)
            mat[i][j] = rand() % MAX;
}

void writeMat(int mat[N][N])
{
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++)
            printf(" %d ", mat[i][j]);

        printf("\n");
    }
    printf("\n\n");
}

int main()
{
    typeMat *matrix1;

    srand(time(NULL));

    newMat(matrix1->matA);
    writeMat(matrix1->matB);


    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
void spoctiSoucet(typMat *mat)
{
    for (int j = 0; j < N; j++) {
        for (int k = 0; k < N; k++) {
            int suma = 0;
            for (int l = 0; l < N; l++)
                suma += (* mat).matA[j][l] * (* mat).matB[l][k];
           (* mat).matSoucet[j][k] = suma;
        }
    }
}
Why is what not working? Post compiler errors, thought process, execution process, etc...

This is any easy one to spot though, matrix1 is a pointer pointing to nothing. You have 2 options:

1
2
3
typeMat *matrix1 = new typeMat();
// or
typeMat matrix1;
Last edited on
but this is C not C++, there is not something like new
closed account (z05DSL3A)
kikolak wrote:
but this is C not C++, there is not something like new

http://www.cplusplus.com/reference/cstdlib/malloc/
Topic archived. No new replies allowed.