问题标签 [const-char]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
621 浏览

c - 在这种情况下,如何使用从函数返回的 const char 指针?(mongodb相关)

我在使用 mongodb c 驱动程序使用的 aa 函数时遇到了一些麻烦。有问题的函数如下所示:

我正在尝试以下列方式使用它:

但是,执行后,我在 test.txt 中看到了这一点:

我不确定我做错了什么。我指定的字段存在于我存储在 gridfs 中的所有文件中,所以我不认为是这种情况(指定不存在的内容只是“文件包含:”中的文件,之后没有字符)。我想我在指针方面做错了。如果有人有任何建议,那就太棒了。

编辑:: 真正的声明是

0 投票
10 回答
438044 浏览

c++ - 如何从 int 转换为 char*?

我知道的唯一方法是:

但是有没有打字少的方法?

0 投票
2 回答
2535 浏览

c++ - C++:将 const char* 类型的字符串文字传递给字符串参数

我是 C++ 新手,不了解为什么这段代码运行良好:

像“bla”这样的字符串文字属于 const char* 类型。至少 auto i = "bla"; 表示 i 的类型为“const char*”。为什么可以将它传递给 GetString-Function,因为该函数需要一个字符串而不是 const char*?

0 投票
3 回答
2700 浏览

c++ - 如何将 const char* 转换为 const WCHAR*

我有这样的事情:

我收到错误:无法将参数 '1' 的 'const char*' 转换为 'const WCHAR*' 到 'DWORD GetFileAttributesW(const WCHAR*)'

如何转换const char*const WCHAR*

0 投票
2 回答
5607 浏览

c++ - 使用 char* 作为 std::map 中的键,它是如何工作的

这个问题与使用 char 作为 stdmap 中的键直接相关。

我了解传入的比较函数的作用以及为什么它需要char *类型作为键。但是,我不确定更新实际上是如何工作的。

我很好奇您正在更新密钥的情况。如何std::map知道如何比较 之间的相等性const char *cmp_str只告诉 map 将键插入树的顺序。

我已经对代码进行了一些挖掘stl_tree.h从这里提取),但找不到太多。我唯一的猜测是它进行了直接的内存比较。

我对下级stl_tree班级如何处理这种情况很感兴趣,或者如果它一直没有正确处理,那么什么情况下会中断?

代码

输出

0 投票
5 回答
25787 浏览

c++ - 自动创建文件名 C++

我正在尝试用 C++ 编写一个程序,它会创建一些文件 (.txt) 并在其中写下结果。问题是这些文件的数量在开始时不是固定的,只出现在程序结尾附近。我想将这些文件命名为“file_1.txt”、“file_2.txt”、...、“file_n.txt”,其中 n 是一个整数。

我不能使用连接,因为文件名需要类型“const char*”,而且我没有找到任何方法将“string”转换为这种类型。我还没有通过互联网找到任何答案,如果你能帮助我,我会很高兴。

0 投票
2 回答
2311 浏览

c++ - Error LNK2001 while using my static library

I'm having LNK2001 issues while building my solution.

The solution has 3 projects, one of them if just a main that returns 0, so I'll exclude it from the problem.

One project (SerialPort) is a .lib that implements a class (SerialPort), while the other (SerialHost) simply creates an instance of SerialPort.

I'm thinking the problem is somewhere in the project settings of SerialHost. Here goes the SerialHost project code:

And building yields:

main.obj : error LNK2001: unresolved external symbol "public: unsigned long __thiscall SerialPort::init(unsigned long,char const *)" (?init@SerialPort@@QAEKKPBD@Z)

I've searched SO for answers and found no solution that cured my problem.

To set the project settings I followed this msnd walkthrough.

I also tried adding specifically the .lib to the Additional Dependencies in Linker-Input.

Then I read this SO thread, and setting the option "Treat wchar_t as build-in type (Properties->C/C++->Language-> ... ) to No still didn't work. I thought it would work because SerialPort::init is declared as

  • it thought the fault was with the use of the LPCTSTR type.

The SerialPort project builds OK (the init method is implemented in the .cpp).

I'm just beginning to work with my own static libs in VS so I may be doing some basic error here.

Thank you in advance for your help!


Additional Info:

I tried commenting out the init and using another method and it built just fine! This code:

in SerialHost just yields the error in init - if I comment init, no linking errors appear.

Here is the implementation for SerialPort

0 投票
1 回答
8827 浏览

c++ - struct 中的 const char 指针声明

我正在尝试这样做,但我的编译器不允许我这样做:

我相信这是因为在结构声明中不能进行任何赋值——它们应该在函数或其他方式中进行。但是我真的不允许分配const char*变量吗?
如果有人可以让我知道我缺少什么,我将非常感激。谢谢

0 投票
1 回答
1986 浏览

c++ - const char * 在循环期间改变值

我有一个函数,它遍历 aconst char *并使用该字符将对象添加到一个实例中,std::map如果它是一系列可识别字符之一。

传递的字符序列是"++++++++++." 前3次迭代,print语句显示'+'、'+'和'+'的期望值,并且的值s继续为“++++++++++” +。”。但是,在第四次迭代中,它变得混乱,产生了奇怪的值,s例如'Ð''öê'、和许多其他字符序列。如果抛出异常的行被删除,并且允许循环继续,那么之后的值不会再改变。'cR ''œk's

其他函数可以访问,s但由于这不是一个多线程程序,我不明白为什么这很重要。我对为什么s会发生变化并不感到困惑,而是为什么它只在第四次迭代时发生变化。

我已经搜索过,唯一看起来完全相关的帖子就是这个,但它仍然没有回答我的问题。(研究一直很困难,因为搜索“const char* 改变值”或类似术语只会出现数百篇关于 is 的部分是 const的帖子)。

最后,我知道我可能应该使用std::string,如果没有答案,我会使用它,但我仍然想了解这种行为。

编辑:

这是调用此函数的代码。

在哪里load

}

并且文件fname++++++++++.传播,使得每行有一个字符。

编辑2:

这是控制台输出的示例:

0 投票
1 回答
1470 浏览

c++ - 用字符串附加 const char 变量

我有 2 个整数,我通过传递给用户定义的函数将其转换为 const char* 。现在我想将这两个变量附加到命令行字符串中

我知道它非常基础,但对数据类型很不好。我如何在此处附加这 2 个 const char?此方法不起作用,因为它会引发错误,表示 const char* 的二进制运算符。请帮助我,尽管它很简陋。

num1 和 num2 是变量