Segmentation Fault with Pointers in C++

只谈情不闲聊 提交于 2019-11-28 14:21:52

You have to create the clientInfo *cI, like

clientInfo *cI = new clientInfo();

clientInfo *cI; declares a pointer to an object of type clientInfo. It does not construct this object. You have to construct it before trying to access its members: cI = new clientInfo();.

The cI pointer is actually pointing "nowhere meaningful to you" when ci->members are assigned. Initilize the pointer to something existent (or allocate something for it) before use it.

You cannot directly assign values to a structure pointer.it should be given some allocation using new like :

 clientInfo *cI =new  clientInfo();

If you dont wish to do this just decalre the object rather than a pointer *cI. like below:

clientInfo cI;

Now you can directly assign values like cI.a=... and this data will be stroed on stack and not on heap.

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