Ver Mensaje Individual
  #2 (permalink)  
Antiguo 17/07/2013, 21:52
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Variable por referencia y malloc

Tu segundo intento es casi perfecto, solamente te falto añadirle paréntesis (prioridad de operadores ).

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void change_value(char ***buffer)
  6. {
  7.     *buffer = malloc(2 * sizeof(char *));
  8.  
  9.     (*buffer)[0] = malloc(5);
  10.     (*buffer)[1] = malloc(5);
  11.  
  12.     strcpy((*buffer)[0], "hola");
  13.     strcpy((*buffer)[1], "chau");
  14. }
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.     char **buffer = NULL;
  19.  
  20.     change_value(&buffer);
  21.     printf("%s, %s\n", buffer[0], buffer[1]);  // <- Segmentation fault
  22.  
  23.     free(buffer);
  24.  
  25.     return 0;
  26. }