[Solucionado]Problemilla al acceder a una matriz dinamica

Iniciado por yiti007, 8 Mayo 2010, 12:32 PM

0 Miembros y 1 Visitante están viendo este tema.

yiti007


ERROR MIO DE SINTAXIS, -.-''

Código (cpp) [Seleccionar]


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

// FUNCION que escanea un numero entero y positivo que despues retorna
int mayor0();
void rellenar(int **,int,int);

int main()
{
int **matriz;
int fila,columna,i;

puts("Introduzca la cantidad de filas deseadas(numero > 0)");
fila = mayor0();
matriz = (int **)malloc(fila*sizeof(int*));
puts("Introduzca la cantidad de columnas deseadas(numero > 0)");
columna = mayor0();
for(i=0 ; i<fila ; i++)
*(matriz+i) = (int*)malloc(columna*sizeof(int));
rellenar(matriz,fila,columna);
}

void rellenar(int **matriz,int fila,int columna)
{
int i, max = fila*columna;
for(i=0 ; i<max ; i++)
*(*(matriz+i)) = mayor0();  
}

int mayor0()
{
int temp = -1;
do
{
scanf("%d",&temp);
fflush(stdin);
}
while(temp<=0);
return temp;
}


Lambda

mayor0 es una funcion se te ha olvidado poner el () xD

leogtz

Estás reservando espacio mal para la matriz:

Debiera ser así:

#include <stdio.h>
#include <stdlib.h>
/*  Esto lo cambias por lo que quieras */
#define FILAS 2
#define COLS 2
int main(void)
{
signed int **matriz;
unsigned int i;
/*  En caso de haber error al alojar: */
if((matriz = malloc(sizeof *matriz * FILAS)) == NULL)
{
perror("Error : ");
exit(EXIT_FAILURE);
}
for(i = 0; i < FILAS; i++)
if((matriz[i] = malloc(sizeof *matriz[i] * COLS)) == NULL)
{
perror("Error : ");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}


El malloc no necesita el casting.
Código (perl) [Seleccionar]

(( 1 / 0 )) &> /dev/null || {
echo -e "stderrrrrrrrrrrrrrrrrrr";
}

http://leonardogtzr.wordpress.com/
leogutierrezramirez@gmail.com