草庐IT

hex_char

全部标签

C++ 异常返回类型为什么是 char*

有人可以解释为什么自写的C++异常,从异常继承返回一个char*而不是一个字符串?classmyexception:publicexception{virtualconstchar*what()constthrow(){return"Myexceptionhappened";}}myex;来源:http://www.cplusplus.com/doc/tutorial/exceptions/ 最佳答案 由于std::exception被设计为所有异常的基类,因此接口(interface)的编写方式使得特化不需要可能抛出的代码。他们可

c# - 如何将带有 unsigned char* 的结构从 C# 传递到 C++?

我有一些带有结构描述和一些方法的C++dll:structMSG_STRUCT{unsignedlongdataSize;unsignedchar*data;}和功能例如:unsignedlongReadMsg(unsignedlongmsgId,MSG_STRUCT*readMsg){readMsg->dataSize=someDataSize;readMsg->data=someData;}所以我想从C#调用这个函数:[StructLayout(LayoutKind.Sequential)]structMSG_STRUCT{UInt32dataSize;byte[]data;}[D

c# - 将 char * 从 C# 字符串传递到 C DLL

我需要在C#应用程序的DLL中使用C函数库。我在使用char*参数调用DLL函数时遇到问题:CDLL:extern"C"__declspec(dllexport)intCopyFunc(char*,char*);intCopyFunc(char*dest,char*src){strcpy(dest,src);return(strlen(src));}C#应用程序需要看起来像这样:[DllImport("dork.dll")]publicstaticexternintCopyFunc(stringdst,stringsrc);intGetFuncVal(stringsource,stri

c++ - c++11 严格别名规则是否允许通过 char *、char(&)[N]、甚至 std::array<char, N>& 使用 -fstrict-aliasing -Wstrict-aliasing=2 访问 uint64_t?

根据this关于C++11/14严格别名规则的stackoverflow回答:Ifaprogramattemptstoaccessthestoredvalueofanobjectthroughaglvalueofotherthanoneofthefollowingtypesthebehaviorisundefined:thedynamictypeoftheobject,acv-qualifiedversionofthedynamictypeoftheobject,atypesimilar(asdefinedin4.4)tothedynamictypeoftheobject,atypet

c++ - 调用 printf 时隐式转换为 char*

我在我的C++GUI应用程序中使用Unicode字符串作为图标,我想摆脱所有散落在周围的u8"\uf118"魔法字符串,并在途中制作这些字符串是它们自己的一种类型。所以我创建了一个这样的类:structicon{explicitconstexpricon(constchar(&unicode_icon)[4]):_icon{unicode_icon[0],unicode_icon[1],unicode_icon[2],unicode_icon[3]}{}operatorconstchar*()const{return_icon.data();}private:std::array_ic

c++ - 从 'const char**' 到 'char* const*' 的无效转换

我正在做一个学校作业,我必须调用execvp,其方法签名如下execvp(constchar*file,char*constargv[])但是我的数据是这样的:std::vector我一直在尝试将所述vector转换为execvp()的第二种格式的正确格式,但不可避免地会出现以下错误:command.cc:120:29:error:invalidconversionfrom‘constchar**’to‘char*const*’[-fpermissive]execvp(args[0],argv);我尝试了不同的变体,但它们都会导致此错误。这个错误让我很困惑,因为我不知道const*是什

c++ - 在最佳时间从 _variant_t 获取 char*

这是我想要加速的代码。它从ADO记录集中获取值并将其转换为char*。但这很慢。我可以跳过_bstr_t的创建吗?_variant_tvar=pRs->Fields->GetItem(i)->GetValue();if(V_VT(&var)==VT_BSTR){char*p=(constchar*)(_bstr_t)var; 最佳答案 BSTR的前4个字节包含长度。您可以遍历并获取每个其他字符(如果是unicode)或每个字符(如果是多字节)。某种memcpy或其他方法也可以。IIRC,这可能比W2A或类型转换(LPCSTR)(_b

c# - 如何使用 P/INVOKE 将 C++ char* 编码为 C# 字符串

我是C++新手。我正在使用PINVOKE从C#调用C++函数,并希望将字符串作为输出参数返回。但是我只得到一个空字符串。intout参数工作正常。导入:[DllImport(@"UnamanagedAssembly.dll",CharSet=CharSet.Ansi)]publicstaticexternintActivate(refintnumActivated,StringBuildereventsActivated);extern"C"__declspec(dllexport)intActivate(int*p_NumActivated,char*p_EventsActivate

c++ - 在 C 中初始化一个 char 数组。哪种方式更好?

以下是初始化char数组的两种方式:charcharArray1[]="foo";charcharArray2[]={'f','o','o','\0'};如果两者等同,人们会希望每个人都使用上面的第一个选项(因为它需要更少的击键)。但是我看到过作者总是不厌其烦地使用第二种方法的代码。我的猜测是,在第一种情况下,字符串“foo”存储在数据段中,并在运行时复制到数组中,而在第二种情况中,字符存储在代码段中,并在运行时复制到数组中.出于某种原因,作者对数据段中的任何内容都过敏。编辑:假设数组声明为局部函数。问题:我的推理是否正确?您喜欢哪种风格?为什么? 最佳答案

c++ - 为什么 "gptr"类型的basic_streambuf是char_type*而不是const char_type*?

设置流缓冲的三个“gptr”的basic_streambuf成员,setg声明为:protected:voidsetg(char_type*gback,char_type*gptr,char_type*egptr);我想知道:为什么每个gptr的类型都是char_type*而不是constchar_type*?在这里使用const_cast为这些gptrs使用constchar指针是否安全? 最佳答案 它不是const,因为streambuf接口(interface)不知道您如何填充缓冲区。例如,underflow和uflow方法可