user2871989
user2871989

Reputation: 65

Two-dimensional Array ERROR: Subscripted value is neither array nor pointer nor vector

So here's my problem:

I would like to SUM Rows in my Array in Function.

Here it is(Function):

int* DodWierszy(int j, int *M, int *M1, int *WynikM)
{
    for(i = 0; i < Kolumny; i++)
    {
        WynikM[j][i] = M[j][i] + M1[j][i];
    }
    return WynikM;
}

And in Main():

for (i = 0; i < Kolumny; i++)
{
    macierzC[i] = DodWierszy(i, macierzA[i], macierzB[i], macierzC[i]);
}

And my error is following:

zmpi.c: In function ‘DodWierszy’:
zmpi.c:13:12: error: subscripted value is neither array nor pointer nor vector
zmpi.c:13:22: error: subscripted value is neither array nor pointer nor vector
zmpi.c:13:33: error: subscripted value is neither array nor pointer nor vector

I am not really similar with C. Please help me out ;<.

Upvotes: 0

Views: 1755

Answers (2)

Artur
Artur

Reputation: 7257

You are doing many things wrong so use my below example for learning purposes.

Here is what you do wrong:

  • use your native language (code) in multinational environment so you limit number of people that might help you so english please
  • DodWierszy does not have to return anything - especially result matrix which is passed to it as param
  • DodWierszy expects pointer to int to be passed and you are not providing it
  • you do not have to pass a row to DodWierszy - you may pass the whole array(ie. pointer to it - not a copy)

Better declare your formal arguments (to DodWierszy) as int matrix [][num of cols].

int matrixA[2/*rows*/][5/*cols*/] = { { 10, 20, 30, 40, 50},
                                      { 9, 8, 7, 6, 5} };

int matrixB[2/*rows*/][5/*cols*/] = { { 1, 2, 3, 4, 5},
                                      { 0, 7, 2, 2, 1} };

int matrixC[2/*rows*/][5/*cols*/] = {0};

void print_matrix(char label[], int p[][5], int rows, int cols)
{
    printf("%s:\n", label);

    for(int r = 0; r < rows; r++)
    {
        for(int c = 0; c < cols; c++)
        {
            printf("%03d ", p[r][c] );
        }
        printf("\n");
    }

    printf("\n");
}

void add_matrix_row(int row, int cols, int a[][5], int b[][5], int c[][5])
{
    for(int col = 0; col < cols; col++)
    {
        c[row][col] = a[row][col] + b[row][col];
    }
}

void main(void)
{
    print_matrix("matrixA", matrixA, 2, 5);
    print_matrix("matrixB", matrixB, 2, 5);
    print_matrix("matrixC", matrixC, 2, 5);

    for (int row = 0; row < 2; row++)
    {
        add_matrix_row(row, 5, matrixA, matrixB, matrixC);
    }

    print_matrix("matrixA", matrixA, 2, 5);
    print_matrix("matrixB", matrixB, 2, 5);
    print_matrix("matrixC", matrixC, 2, 5);
}

result:

matrixA
010 020 030 040 050
009 008 007 006 005

matrixB:
001 002 003 004 005
000 007 002 002 001

matrixC:
000 000 000 000 000
000 000 000 000 000

matrixA:
010 020 030 040 050
009 008 007 006 005

matrixB:
001 002 003 004 005
000 007 002 002 001

matrixC:
011 022 033 044 055
009 015 009 008 006

Instead of passing the whole array you may pass a row:

for (int row = 0; row < 2; row++)
{
    add_matrix_row(matrixA[row], matrixB[row], matrixC[row]);
}

then

void add_matrix_row(int a[5], int b[5], int c[5])
{
    for(int col = 0; col < 5; col++)
    {
        c[col] = a[col] + b[col];
    }
}

Upvotes: 1

prmottajr
prmottajr

Reputation: 1824

In:

WynikM[j][i] = M[j][i] + M1[j][i]; 

you are addressing like a 2d array (aka matrix) but it seems that you are passing 1d arrays in the function call.

Upvotes: 0

Related Questions