puzzlekid
puzzlekid

Reputation: 11

initializing a matrix, expected expression error in C

So I have tried looking up a few things that seemed helpful, but ultimately were not.

I am supposed to initialize a 6x8 matrix as a 2D array in a separate function. I have three files, a main function file, a functions file, and a header file.

Here is relevant code from main

     int plate[MAX_ROWS][MAX_COLS];

double A = 0, B = 0, T1 =0, T2 = 0, C = 0;

printf("\n");
printf("Welcome to the Heat Plate Simulation\n\n");
printf("Enter: Heat-A, Heat-B, Plate-1, Plate-2, Stab-Crit\n\n");
scanf("%lf%lf%lf%lf%lf", &A,&B,&T1,&T2,&C);

then I try

    initialize_plate(plate,T1, T2);

which goes to

    void initialize_plate(int plate[][MAX_COLS],double T1, double T2)
    {
 plate[MAX_ROWS][MAX_COLS] = {
    { T1, T1, T1, T2, T2, T2},
    { T1, T1, T1, T2, T2, T2},
    { T1, T1, T1, T2, T2, T2},
    { T2, T2, T2, T1, T1, T1},
    { T2, T2, T2, T1, T1, T1},
    { T2, T2, T2, T1, T1, T1} 
    };
return;
    }

The error I receive is 7P_functions.c:14:32: error: expected expression before ‘{’ token *plate[MAX_ROWS][MAX_COLS] = {

Given what I have researched with this error my only guess is that it has something to do with the array already being initialized, but I am not sure how to rectify this problem whereas if I initialize the array within the function and not in main doesn't it just become a local variable?

Any help would be much appreciated.

Upvotes: 1

Views: 730

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

Set the value using memcpy from the temporary array.

void initialize_plate(int plate[][MAX_COLS],double T1, double T2){
    memcpy(plate, (int [][MAX_COLS]){
        { T1, T1, T1, T2, T2, T2},
        { T1, T1, T1, T2, T2, T2},
        { T1, T1, T1, T2, T2, T2},
        { T2, T2, T2, T1, T1, T1},
        { T2, T2, T2, T1, T1, T1},
        { T2, T2, T2, T1, T1, T1} },
        6*sizeof(int [MAX_COLS]));

    return;
}

Upvotes: 1

tinyfiledialogs
tinyfiledialogs

Reputation: 1355

this plate[MAX_ROWS][MAX_COLS] = {...}; tries to reserve a place in memory, with initialization. you can't use this method to modify the values of an array.

Upvotes: 0

Related Questions