草庐IT

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++ - 错误 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++ - 当 vector<char>&& 绑定(bind)到 vector<char>& 时编译器不会报错

我使用的是VisualStudio2013Express。classB{public:vector&a;int&b;B(vector&i,int&c):a(i),b(c){}};intmain(){intl=3;vectorh;shared_ptrbb(newB(std::move(h),l));return0;}为什么代码可以被接受?当我将参数l更改为std::move(l)时,编译器会报错“无法将参数2从'int'转换为'int&'”。 最佳答案 这是VisualC++编译器中可用的语言扩展,现在已经存在了很长一段时间。该扩展允

c++ - 使用对 const char * 的右值引用的重载解析

#includeusingnamespacestd;voidf(constchar*const&s){cout输出:rvaluervalue为什么输出不是“右值左值”? 最佳答案 字符串文字和s都不是指针(它们是数组),因此标准的相关部分是[conv.array]:Anlvalueorrvalueoftype"arrayofNT"or"arrayofunknownboundofT"canbeconvertedtoaprvalueoftype"pointertoT".Theresultisapointertothefirsteleme