Home > Mobile >  expected expression ,what is expected here
expected expression ,what is expected here

Time:07-19

int **A;
    A=(int**)malloc(3*sizeof(int*));
    
    A[0]=(int *)malloc(4*sizeof(int));
    A[1]=(int *)malloc(4*sizeof(int));
    A[2]=(int *)malloc(4*sizeof(int));
    
    A[3][4]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
    
    for(int i=0;i<3;i  )
    {
        for(int j=0;j<4;j  )
            printf("%d ",A[i][j]);
        printf("\n");
    }

}

Here it is showing that "expected expression" what should I do please help me out.

CodePudding user response:

I see no reason to do more than one malloc here (and perhaps not even that one).

In the below you try to assign all values to one single element in the 2D array (and it's also out of bounds since A[2][3] is the last accessible element).

A[3][4]={{1,2,3,4},{1,2,3,4},{1,2,3,4}}

What you could do is to memcpy instead.

Example:

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

int main() {
    int(*A)[4] = malloc(3 * sizeof *A); // only one allocation

    // "assign" all values to where A points:
    memcpy(A, (int[3][4]){{1, 2, 3, 4},{1, 2, 3, 4},{1, 2, 3, 4}}, 3 * sizeof *A);

    for (int i = 0; i < 3; i  ) {
        for (int j = 0; j < 4; j  ) printf("%d ", A[i][j]);
        printf("\n");
    }

    free(A); // only one deallocation
}

CodePudding user response:

A[3][4]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};

It is definitely wrong.

you can assign the pointer element of the allocated array with reference to the existing array or compound literal. But it will not possible to use it as 2D array as per your pseudo code, only 1D array.

A[3]=(int[]){1,2,3,4,5,6,7,8,9,0};

or

int B[] = {1,2,3,4,5,6,7,8,9,0};
A[3] = B;

A[3] is out of bounds so you need to change the malloc as well.

A = malloc(4 * sizeof(*A));

Also do not cast the result of malloc. If your code does not compile, it means that you are using C compiler to compile C code which is not correct

CodePudding user response:

A[3][4]={{1,2,3,4},{1,2,3,4},{1,2,3,4}}; is wrong. I think you are trying to assign something to the whole array. There are at least a couple of things wrong with that:

  1. Arrays cannot be assigned to like that. The syntax you used is for initialization (as part of a declaration), not assignment (as part of an expression statement).
  2. A is not a 2-D array of int, it is a dynamically allocated array of int * whose elements point to (the first elements of) dynamically allocated arrays of int.

The elements need to be assigned individually (or via memcpy), for example using a couple of for loops:

    for (int i = 0; i < 3; i  )
    {
        for (int j = 0; j < 4; j  )
        {
            A[i][j] = j   1;
        }
    }

If A was an actual 2-D array of int, it could be declared and initialized using an initializer:

    int A[3][4]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};

CodePudding user response:

For starters bear in mind that it is not a good style of programming to use uppercase letters for identifiers of variables.

In this statement

A[3][4]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};

you are trying to assign values in the braced list to the scalar object of the type int that even does not exists because the indices 3 and 4 access memory outside the dynamically allocated arrays.

So as the above expression statement uses the assignment operator then the compiler expects that the right operand of the assignment is an expression. Braced lists are not expressions. So the compiler issues an error.

Pay attention to that arrays do not have the assignment operator. You need to copy elements of one array into another or set elements individually.

You could initially declare an array and initialize it with the braced list like

int A[3][4]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};

As for dynamically allocated arrays then you could initialize their elements in for loops like for example

for ( size_t i = 0; i < 3; i   )
{
    int value = 1;
    for ( size_t j = 0; j < 4; j   )
    {
        A[i][j] = value  ;
    }
}

Another approach is to use compound literals like

for ( size_t i = 0; i < 3; i   )
{
    memcpy( A[i], ( int[] ){ 1, 2, 3, 4 }, sizeof( int[4] ) );
}

Here is a demonstration program.

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

int main( void )
{
    enum { M = 3, N = 4 };
    int **a = malloc( sizeof( int *[M] ) );

    for ( size_t i = 0; i < M; i   )
    {
        a[i] = malloc( sizeof( int[N] ) );
        memcpy( a[i], ( int[N] ){ 1, 2, 3, 4 }, sizeof( int[N] ) );
    }

    for ( size_t i = 0; i < M; i   )
    {
        for ( size_t j = 0; j < N; j   )
        {
            printf( "%d ", a[i][j] );
        }
        putchar( '\n' );
    }

    for ( size_t i = 0; i < M; i   )
    {
        free( a[i] );
    }
    free( a );
}

The program output is

1 2 3 4 
1 2 3 4 
1 2 3 4

In general you should check whether memory was allocated successfully.

  • Related