草庐IT

char_length

全部标签

c++ - 使用流提取到 char 指针时出现段错误

我有一个问题。我有以下结构:typedefstruct{intvin;char*make;char*model;intyear;doublefee;}car;然后我有以下方法询问用户汽车的品牌并将其作为字符指针返回:char*askMake(){char*tempMake=NULL;cout>tempMake;returntempMake;}然后我有一辆临时车struct:cartempCar;我正在尝试以这种方式为其分配一个值:tempCar.make=askMake();它编译得很好,但我在运行时遇到了段错误。 最佳答案 您还没

c++ - 错误 : Variable length array of Non-POD element type 'string'

在开始之前,我必须首先声明,我已经研究过针对此错误的可能解决方案。不幸的是,它们都与不使用数组有关,这是我项目的要求。另外,我目前正在学习CS入门类(class),所以我的经验几乎没有。数组的用途是从文件中收集名称。因此,为了初始化数组,我计算了名称的数量并将其用作大小。问题是标题中所述的错误,但我仍然使用一维数组时看不到解决方法。主要.cpp#include#include#include#include#include#include"HomeworkGradeAnalysis.h"usingnamespacestd;intmain(){ifstreaminfile;ofstrea

c++ - 错误 C2440 : '=' : cannot convert from 'const char [2]' to 'char'

我正在学习C++,但我在做一些新手的事情时遇到了问题。我正在尝试创建一个非常小的应用程序,它接受用户输入并将其存储到一个char数组中。然后我解析该数组并删除所有括号和dases并显示它。像下面这样(325)858-7455到3258587455但是我遇到了错误errorC2440:'=':cannotconvertfrom'constchar[2]'to'char'下面是我的简单代码,可以很容易地放入编译器并运行。#include"stdafx.h"#include#includeusingnamespacestd;/*ThisisatemplateProject*/intmain(

c++ - int 和 char 数组有什么区别?

下面的int和char数组有什么区别:intmain(){intnumbers[]={2,1,3};charletter[]={'a','b','\0'};cout输出:0x22ff12//anaddressab为什么不显示213?我知道数组的名称将指向其第一个元素的地址,但为什么char数组显示不同的行为吗? 最佳答案 没有operator确切地说,采用数组的重载,因此您传递的参数(例如numbers和letter)经过数组到指针的转换,变为void*和char*分别。operator过载这需要constvoid*,另一个需要co

c++ - 将 const char * 转换为 std::string

这个问题在这里已经有了答案:Howtoconvertaconstchar*tostd::string[duplicate](6个答案)关闭8年前。我有一个从处理函数返回的constchar*,我想将它转换/分配给std::string的实例进一步操纵。这看起来应该是直截了当的,但我找不到任何说明应该如何完成的文档。显然,我错过了一些东西。见解赞赏。

c++ - g++ 将返回的字符串文字视为 const char 指针而不是 const char 数组

当从应该使用g++(版本4.7.3)执行隐式转换的函数返回字符串文字时,我看到了一些奇怪的行为。任何人都可以解释为什么以下代码:#includeclassTest{public:templateTest(constchar(&foo)[N]){printf("Templateconstchararrayconstructor\n");}Test(char*foo){printf("char*constructor\n");}};Testfn(){return"foo";}intmain(){Testt("bar");Testu=fn();return0;}产生结果:Templateco

c++ - 我不明白在下面的代码中将 char buffer[] 与 X 类型的对象对齐的原因

Stroustrup在他的新书第151页中展示了以下使用类型说明符alignas的示例:Sometimes,wehavetousealignmentinadeclaration,whereanexpression,suchasalignof(x+y)isnotallowed.Instead,wecanusethetypespecifieralignas:alignas(T)means"alignjustlikeaT."Forexample,wecansetasideuninitializedstorageforsometypeXlikethis:voiduser(constvector

c++ - 从 *char 数组转换时有哪些严格的别名规则?

在将char数组转换为其他类型时,我对严格的别名规则感到困惑。我知道允许将任何对象转换为char数组,但我不确定反过来会发生什么。看看这个:#includeusingnamespacestd;struct{alignas(int)charbuf[sizeof(int)];//correct?}buf1;alignas(int)charbuf2[sizeof(int)];//incorrect?struct{floatf;//obviouslyincorrect}buf3;typenamestd::aligned_storage::typebuf4;//obviouslycorrecti

c# - 如何将带有 const char* 的 C union 映射到 C# 结构?

在本地库的回调函数中,我需要访问一个espeak_EVENT数组。问题出在原C代码中的UNION语句:typedefstruct{espeak_EVENT_TYPEtype;unsignedintunique_identifier;//messageidentifier(or0forkeyorcharacter)inttext_position;//thenumberofcharactersfromthestartofthetextintlength;//wordlength,incharacters(forespeakEVENT_WORD)intaudio_position;//th

c++ - 在抛出 'std::length_error' 实例后调用终止

这是我在这里的第一篇文章。由于我是新手,这个问题可能很愚蠢。当显示以下错误消息时,我正在编写一段代码,在抛出“std::length_error”的实例后调用终止什么():basic_string::_S_create/home/gcj/finals/home/gcj/quals中止的地方以下是有问题的代码,尤其是第39行到第52行。这对我来说很奇怪,因为这段代码几乎与第64行到第79行相同。intmain(){std::vectordirs,need;std::stringtmp_str;std::ifstreamfp_in("small.in");std::ofstreamfp_o