1

here is my code:

int main(int argc, const char * argv[])
{

    int *A[3] = {{1,1,1,1},{1,1,1,1},{1,1,1,1}};
    int *B[4] = {{2,2,2},{2,22,2},{3,3,3},{3,3,3}};
    int *C[3];
    multiply(A, 1, 2, B, 3, 4, C);
    printf("A matrix:\n");
    printMatrix(A,3,4);
    printf("B matrix:\n");
    printMatrix(B,4,3);
    printf("C matrix:\n");
    printMatrix(C,3,3);
    printf("Hello, World!\n");
    return 0;
}

Its just int main part!

I'm getting the error

[warning]Braces around scalar initializer

in line:

int *A[3] = {{1,1,1,1},{1,1,1,1},{1,1,1,1}};

Why? Is it because of my mistake or compiler version difference?

1
  • 2
    A is array of int*. Commented Apr 12, 2014 at 20:27

1 Answer 1

4

A is an array of int *, not a 2D array. Therefore, the initializers should be scalar values which are an int *.

If you have C99 or C11, you can rescue the code like this:

#include <stdio.h>

extern void printMatrix(int *x[], int y, int z);
extern void multiply(int *A[], int n1, int n2, int *B[], int n3, int n4, int *C[3]);

int main(void)
{
    int *A[3] = {(int []){1,1,1,1}, (int []){1,1,1,1}, (int []){1,1,1,1}};
    int *B[4] = {(int []){2,2,2}, (int []){2,22,2}, (int []){3,3,3}, (int []){3,3,3}};
    int *C[3];
    multiply(A, 1, 2, B, 3, 4, C);
    printf("A matrix:\n");
    printMatrix(A, 3, 4);
    printf("B matrix:\n");
    printMatrix(B, 4, 3);
    printf("C matrix:\n");
    printMatrix(C, 3, 3);
    printf("Hello, World!\n");
    return 0;
}

This uses C99 compound literals in the initializers.

However, you will need to be careful to allocate the right amount of space inside multiply() to store the data for each row in C. You'll also have to know how to release the allocated space.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.