Getting segmentation fault strcpy [duplicate]

痞子三分冷 提交于 2019-12-25 05:11:14

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/22885961/getting-segmentation-fault-strcpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!