Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 创建C字符串的最佳方法_C++_C_String - Fatal编程技术网

C++ 创建C字符串的最佳方法

C++ 创建C字符串的最佳方法,c++,c,string,C++,C,String,我目前正在使用 char *thisvar = "stringcontenthere"; 在C中声明字符串 这是在C中声明字符串的最佳方法吗 和从C++字符串生成C字符串如何?< P>这是C还是C++?在C++中,你应该使用: 正如其他人所建议的,如果您想用C++的方式“做”,请使用std::string 如果您不知何故需要一个C-string,std::string有一个提供const char*的方法 以下是一个例子: #include <iostream> #include

我目前正在使用

char *thisvar = "stringcontenthere";
在C中声明字符串

这是在C中声明字符串的最佳方法吗


<>和从C++字符串生成C字符串如何?

< P>这是C还是C++?在C++中,你应该使用:


正如其他人所建议的,如果您想用
C++
的方式“做”,请使用
std::string

如果您不知何故需要一个
C-string
std::string
有一个提供
const char*
的方法

以下是一个例子:

#include <iostream>
#include <string>

void dummyFunction(const char* str)
{
  // Do something
}

int main(int, char**)
{
  std::string str = "hello world!";

  dummyFunction(str.c_str());

  return EXIT_SUCCESS;
}
#包括
#包括
void dummy函数(const char*str)
{
//做点什么
}
int main(int,char**)
{
std::string str=“你好,世界!”;
dummyFunction(str.c_str());
返回退出成功;
}

在C语言中,这取决于如何使用字符串:

  • 命名常量:您的
    char*str=“string”方法可以(但应该是
    字符常量*
  • 要传递给子函数但在调用函数返回后不会使用的数据:
    charstr[]=“string”
  • 在退出中声明函数后将使用的数据:
    char*str=strdup(“字符串”),并确保它最终得到免费的

如果这还没有涵盖,请尝试在答案中添加更多细节。

视情况而定。对于ASCII编码的字符串,请参见段落C和C++。有关unicode编码字符串,请参见最后一段

C: 正如David指出的,这取决于如何在C中使用字符串:

  • 作为常量:
    constchars[]=“helloworld”
  • 作为包含变量数据的字符串:
    chars[]=“Hello World”
  • 作为数据数组
    char*data
    ;然后应该自定义初始化
请注意,在C中,所有字符串都以Null结尾,这意味着定义,例如
chars[]=“foo”
s[3]='\0'
末尾隐式包含一个
NULL
字符

另外,请注意
char*s
char*s[]
之间的细微差别,它们的行为通常相同,但有时不同!(见)例如:

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char* argv[])
{
  char s[] = "123456789123456789123456789";
  char *t = (char*) malloc( sizeof(char) * 28 );
  for( size_t i = 0; i < 28; ++i )
      t[ i ] = 'j';
  printf( "%lu\n", sizeof(s) );
  printf( "%lu\n", sizeof(t) );
  printf( "%s\n", s );
  printf( "%s\n", t );
  return EXIT_SUCCESS;
}
Unicode: 如果你想要unicode字符串,那么你真的应该使用

char utf8String[] = u8"Hello World";

尽可能不要使用
wchar\t
。请参阅这篇关于该问题的优秀文章:。请不要对C++也有Unicode支持。但总的来说,我想说的是,你应该尽可能地使用普通角色。有趣的资源:

它必须读取thisvar=“stringcontenthere”,它应该是const char*然后它应该被命名为
const char*thiscont=“stringcontenthere”序列号;请注意,这应该是
const char*thisvar=“stringcontenthere”(附加的
常数
)。有一个隐式的
const
-删除从字符串文本到
char*
的转换,但这是不推荐的,不应该使用
定义一个变量(类型为
char*
)和
char*foo=“bar”定义并初始化变量。声明应该是
extern char*foo。(请参阅定义和声明是什么)。您应该明确是否对C++也感兴趣,因为C++标签在问题中没有发现任何注意事项。
#include <stdio.h>
#include <stdlib.h>

int main( int argc, char* argv[])
{
  char s[] = "123456789123456789123456789";
  char *t = (char*) malloc( sizeof(char) * 28 );
  for( size_t i = 0; i < 28; ++i )
      t[ i ] = 'j';
  printf( "%lu\n", sizeof(s) );
  printf( "%lu\n", sizeof(t) );
  printf( "%s\n", s );
  printf( "%s\n", t );
  return EXIT_SUCCESS;
}
std::string s = "Hello World";
your_c_call( s.c_str(), ... );
char utf8String[] = u8"Hello World";