문제

struct Object * newObj(char * nome, int idade, float altura) {
    struct Object *obj = (struct Object *) malloc(sizeof(struct Object));
    strcpy(obj->nome, nome); // This is the line
    obj->idade = idade;
    obj->altura = altura;
    return obj;
}

This is my code, I don't know why I'm getting segmentation fault in strcpy.
Any ideas?

Thanks in advance.

도움이 되었습니까?

해결책

In your struct Object type, the nome member is declared as a pointer and you also need to allocate memory for the array. Without allocating memory obj->nome has an inderminate value.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top