2
I have the following code:
typedef struct {
     unsigned int ordem;
     int *elementos;
}MAT_DIAG;
void criar_matriz (MAT_DIAG *m, int d);
void inicializar_matriz (MAT_DIAG *m);
void imprimir_matriz (MAT_DIAG *m);
int retornar_elemento_matriz (MAT_DIAG *m, int i, int j);
void main(){
    int ord, o=1;
    MAT_DIAG *d;
    printf ("Qual e a ordem da matriz? ");
    scanf ("%d", &ord);
    while ((o>=1)&&(o<=2)){
    printf ("\n");
    printf ("1. Criar e inicializar matriz\n");
    printf ("2. Imprimir matriz\n");
    printf ("Escolha a opcao: ");
    scanf ("%d", &o);
    printf ("\n");
    switch (o){
        case 1: criar_matriz(d, ord);
                inicializar_matriz(d);
        break;
        case 2: imprimir_matriz(d);
        break;
        default: printf ("Opcao invalida");
                exit(1);
    }
}
}
void criar_matriz(MAT_DIAG *m, int d){
    m->ordem=d;
    m->elementos = (int *)malloc(sizeof(int)*d);
    if (!m->elementos){
        printf ("Nao foi possivel reservar memoria para a matriz!\n");
        exit(2); }
}
void inicializar_matriz(MAT_DIAG *m){
    int i;
    printf ("\nEntre com os elementos da diagonal principal da matriz: \n");
    for (i=0;i<m->ordem;i++){
        printf ("matriz[%d][%d]",i+1,i+1);
        scanf ("%d",m->elementos+i);}
}
void imprimir_matriz(MAT_DIAG *m){
    int i, j;
    for (i=0;i<m->ordem;i++){
        printf ("\n");
        for (j=0;j<m->ordem;j++){
            printf ("%05d ", retornar_elemento_matriz(m,i,j)); 
        }
    }
}
I don’t know why, but when I compile it says the program stopped working. D:
Why do you use that condition
(o>=1)&&(o<=2)?– stderr
For the menu to keep repeating while the user type the two options
– poirot
Don’t forget to do
free()to memory allocated when you no longer need it.– pmg
I know you’ve already received some answers, but the first step in these cases is to run the program step by step in a debugger and add the information you got through the debugger to the question, including reducing the code to relevant areas in order to speed up the evaluation of the code.
– Vinícius Gobbo A. de Oliveira