-1

I can't make the function mostrarmatriz() work. The program does compile and run from beginning to end without errors, but the function mostrarmatriz() doesn't show up anything. When I copy that function into the main function it works perfectly. It seems I'm having a problem passing the values by reference. Please help, I'm stuck.

Main.c File:

#include "InvMatriz.h"
#include <stdio.h>

int main(int argc, char **argv)
{
    float **A;
    int n;
    printf("Ingrese el tamaño de la matriz: ");
    scanf("%d", &n);

    A = ingresematriz(n);
    void mostrarmatriz(A, n);
    printf("adfasd");

    return 0;
}

Header File InvMatriz.h

#ifndef INVMATRIZ_H_
#define INVMATRIZ_H_

float** ingresematriz(int );
void mostrarmatriz(float**X ,int x);

#endif // INVMATRIZ_H

Ingrese Matriz.c File

#include "InvMatriz.h"
#include <stdio.h>



float **ingresematriz(int n)
{

    int i, j;

    //Asigna espacio en la memoria

    float **A;

    A = malloc(n * sizeof (float *));
    for (i = 0; i < n; i++)
    {
        *(A + i) = malloc(n * sizeof(float));
    }

    //Pide los elementos y los guarda

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Elemento [%d] [%d]:  ", i + 1, j + 1);
            scanf("%f", *(A + i) + j);
        }
    }
    return A;
}

Mostrar Matriz.c File

#include "InvMatriz.h"
#include <stdio.h>

void mostrarmatriz(float **X , int x) //Porque no hace nada pero si compila?
{

    int i, j;
    for (i = 0; i < x; i++)
    {
        for (j = 0; j < x; j++)
        {
            printf("%f        ", *(*(X + i) + j));
        }
        printf("\n");


    }
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ivanz
  • 15
  • 1
  • 5
  • There is no such thing as pass-by-reference in C. Just pointing that bit out. It's ALSO worth noting that aside from the fact that "Your function doesn't work" several other bits of your functions are seriously lacking, and I'm amazed that your malloc() calls aren't generating SOME sort of compiler warning. – Happington Jun 13 '14 at 18:36
  • Isn't a pointer a reference? – Ivanz Jun 13 '14 at 18:39
  • 1
    The answer provided below by Daniel is actually the correct reason your function isn't being called. And no, it's not pass by reference. A pointer is the value of the address where the variable has been stored. You are not passing a reference to a variable, you are passing a value, this value is an address where the variable lives. This may seem like being picky, but there are some huge differences. – Happington Jun 13 '14 at 18:41
  • As it's unrelated to your problem, I don't want to post it as an answer but *(A+i)=malloc(n*sizeof(float));, *(A+i) should be replaced with A[i]. Small detail, but on 64 bit systems, where a pointer is 2 bytes, you'll be in trouble. – Happington Jun 13 '14 at 18:44
  • @Happington Curious: from a _caller's_ point-of-view with `int a[5]; foo(a);` how is this different from "pass-by-reference"? – chux - Reinstate Monica Jun 13 '14 at 18:44
  • Here's ONE example, but a quick search on stack overflow for the differences between pointers and references will return dozens of results. Just as a note, all will mention PBR in C++ context, as C doesn't actually HAVE references. http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c – Happington Jun 13 '14 at 18:47
  • @Happington My query does not have any pointers in it, from the _caller's_ POV - Not sure why then reference a C++ query about PBR vs. pointers? That SO post and others well discuss the difference between PBR and passing by pointers considering the receiving function's differences. Yes, C does not have PBR in total, but from the _limited view of the caller_, my comment related to how does `int a[5]; foo(a);` differ from PBR - Still do not see any difference. – chux - Reinstate Monica Jun 13 '14 at 19:14
  • "Passing a pointer simulates Pass By Reference, but is NOT pass by reference, you are passing the value of the pointer." So no FUNCTIONAL difference, but an extremely important difference when it comes to details. It's easy to forget if you're not careful in C++ to fall into instances that would segfault, or cause heap corruption (deletes, etc.) Also, when you foo(a), the a IS a pointer. – Happington Jun 14 '14 at 02:51

1 Answers1

5

The line

void mostrarmatriz(A,n);

does not call mostrarmatriz, but it declares a function void mostrarmatriz(int,int) (the int type for the arguments is the old "implicit int" rule). Remove the void in that line, and your function will be called.

If you compiled your code with warnings, your compiler would have told you.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431