char *类型的值不能用于初始化类型为“ char”的实体。

char *类型的值不能用于初始化类型为“ char”的实体。

问题描述:

我是C ++的新手,我想做一些简单的事情,例如将char的内容写入磁盘[]

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

我很难

这是我的代码:

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:类型 char *的值不能用于初始化类型为 char的实体

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

您不能将char数组(x)放在char列表中。如错误消息所述。如果要将一个char数组复制到另一个char数组,请参见strcat系列函数。

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.

下一个错误涉及sizeof。它不计算字符串的长度。现在,当您使用它时,它将给出4:缓冲区指针的大小。有一个strlen函数用于获取C字符串的长度。

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.