Value of type char* cannot be used to initialize an entity of type “char”

橙三吉。 提交于 2020-01-07 06:46:10

问题


I'm new to C++ and I'd like to do simple stuff such as writting to disk the content of a char[]

I'm having hard times doing it.

Here's my code:

char x[256],y[256],z[256];

        sprintf( x, "%.2f", pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.x ); //pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.x  is a float struct


        sprintf( y, "%.2f", pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.y ); //pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.y  is a float struct


        sprintf( z, "%.2f", pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.z ); //pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.z  is a float struct

    FILE *tracker_file = fopen("NDI_FiMe.TMP","w");
                char buffer[] = {x,";",y,";",z};
                fwrite(buffer , sizeof(char), sizeof(buffer), tracker_file);
                fclose(tracker_file);

The problem I'm having is that I get:

IntelliSense: a value of type "char *" cannot be used to initialize an entity of type "char"


回答1:


You can't put a char array (x) in a list of chars. As the error message says. If you want to copy a char array into another char array see the strcat family of functions.

The next error involves sizeof. It does not compute the length of a string. As you are using it now it will give a 4: the size of the buffer pointer. There is a strlen function for getting the length of C string.



来源:https://stackoverflow.com/questions/24254183/value-of-type-char-cannot-be-used-to-initialize-an-entity-of-type-char

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