char *类型的值不能用于初始化类型为&"char&"的实体. [英] Value of type char* cannot be used to initialize an entity of type "char"

查看:118
本文介绍了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.

这篇关于char *类型的值不能用于初始化类型为&"char&"的实体.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆