Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code keeps telling me expected expression before \'{\' token on every line

ID: 3672782 • Letter: T

Question

This code keeps telling me expected expression before '{' token on every line where Array[j]={....}; what am I missing?

int** Array = malloc(sizeof(int)*Rsize);
for(i=0;i<Rsize;i++)
Array=calloc(3,sizeof(int));

switch (i) {

case 0 :

Array[j]={1,1,1};

j++;

break;

case 1 :

Array[j]={1,1,0};

j++;

break;

case 2 :

Array[j]={1,0,1};

j++;

break;

case 3 :

Array[j]={1,0,0};

j++;

break;

case 4 :

Array[j]={0,1,1};

j++;

break;

case 5 :

Array[j]={0,1,0};

j++;

break;

case 6 :

Array[j]={0,0,1};

j++;

break;

default :

Array[j]={0,0,0};

j++;

}

Explanation / Answer

You were missing two things. First one was in using calloc, second one was you need to cast {0,0,1} and rest using int[3] and then point it to the pointer Array[j]


#include <stdio.h>
#include <stdlib.h>


int main()
{

    int i,j=0,Rsize = 10;

    int** Array = malloc(sizeof(int)*Rsize);
    for(i=0;i<Rsize;i++)
    {
        Array[i]=calloc(3,sizeof(int));

        switch (i) {

            case 0 :
            Array[j]=(int [3]){1,1,1};
            j++;
            break;

            case 1 :
            Array[j]=(int [3]){1,1,0};
            j++;
            break;

            case 2 :
            Array[j]=(int [3]){1,0,1};
            j++;
            break;

            case 3 :
            Array[j]=(int [3]){1,0,0};
            j++;
            break;

            case 4 :
            Array[j]=(int [3]){0,1,1};
            j++;
            break;

            case 5 :
            Array[j]=(int [3]){0,1,0};
            j++;
            break;

            case 6 :
            Array[j]=(int [3]){0,0,1};
            j++;
            break;

            default :
            Array[j]=(int [3]){0,0,0};
            j++;

        }
    }

    for(i=0;i<Rsize;i++)
        {
            for(j=0;j<3;j++)
                printf("%d ", Array[i][j]);
            printf(" ");
        }

        return 1;
}