I am getting an error message that I don't know how to fix

nombre is a pointer to the structure Nombre, so nombre[0] is the structure, not an integer.

You should allocate correct size and refer the member nombre to access the elements.

Also note that casting results of malloc() family is considered as a bad practice.

#include <stdlib.h>

typedef struct{
  char nombre[9];
}Nombre;

Nombre* crearNombre(){
    Nombre *nombre;
    nombre = malloc(sizeof(*nombre));
    if (nombre == NULL) return NULL;
    nombre->nombre[0] = 'A'+(rand()%26);
    for (int i=1; i<9; ++i){
        nombre->nombre[i] = 'a'+(rand()%26);
    }
    return nombre;
}

One more point: I removed the if(i == 9) statement because i will never be 9 under the loop condition i < 9.

Leave a Comment