error: expected expression before 'matrix'

0

The program should read an array from the keyboard but pull this error

  

error: expected expression before 'array'

#include <stdio.h>
#define TAM 3
typedef int matriz[TAM][TAM];

int main()
{
    int i, j;
    for(i=0; i<TAM; i++)
    {
        for(j=0; j<TAM; j++)
        {
            printf("Ingrese el elemento [%d,%d] ",i,j);
            scanf("%d",&matriz[i][j]);
        }
    }
}

void Mostrar (matriz M)  
{
    int i,j ;    
    for (i=0; i<TAM; i++) 
        for (j=0; j<TAM; j++)
            printf("%d",M[i][j]);
}
    
asked by Alejandro Caro 27.11.2017 в 03:00
source

1 answer

0

You have a little cocoa. You want matriz to behave as a variable and as a type at the same time and that is not possible:

scanf("%d",&matriz[i][j]); // <<--- variable

void Mostrar (matriz M)  // <<--- tipo

For this answer we will assume that your intention is that matriz is a type.

typedef serves to define an alias and its syntax is:

typedef [alias] [tipo];

In your case the program understands that int is an alias and matriz[TAM][TAM] is the type ... and of course, that of matriz the compiler sounds like Chinese because it is not a valid type.

The code should look like this:

typedef matriz int[TAM][TAM];

Now we have to correct the main function since right now it is assuming that matriz is a variable ... when it really is a type. The first thing to do is declare a variable:

int main()
{
    matriz M;

    for(int i=0; i<TAM; i++)
    {
        for(int j=0; j<TAM; j++)
        {
            printf("Ingrese el elemento [%d,%d] ",i,j);
            scanf("%d",&M[i][j]);
        }
    }

    Mostrar(M); // <<--- Esto te falta
}

And now, with these changes, the program should compile without problems.

    
answered by 27.11.2017 / 09:36
source