Best way to create a C string

I am currently using

char *thisvar = "stringcontenthere"; 

to declare a string in C.

Is this the best way to declare a string in C?

What about creating a C-String from C ++ - Strings?

+7
c ++ c string
source share
5 answers

In C, it depends on how you use the string:

  • named constant: your method char* str = "string"; ok (but should be char const* )
  • data is transferred to the subfunction, but will not be used after the call function returns:
    char str[] = "string";
  • which will be used after the function is declared in the outputs: char* str = strdup("string"); , and make sure she finally gets free d.

if that doesn't cover it, try adding more details to your answer.

+13
source share
 const char *thisvar="stringcontenthere"; 
+1
source share

Like others, and I want to “do it” in C++ way, use std::string .

If you need a C-string somehow, std::string has a method that gives const char* .

Here is an example:

 #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; } 
+1
source share

It depends. For ASCII encoded strings, see paragraphs C and C ++. For unicode-encoded strings, see the last paragraph.

C:

As David noted, it depends on how you use the string in C:

  • as a constant: const char s[] = "Hello World";
  • as a string containing variable data, then: char s[] = "Hello World";
  • as an array of data char *data ; Initialization must then be configured.

Note that in C all lines are null-terminated, this means a definition, for example. char s[] = "foo"; implicitly includes the NULL character at the end of s[3]='\0' .

Also note the difference in subtlety between char *s and char s[] , which can often behave the same, but sometimes not! (see Is the name of the array a pointer? ), for example:

 #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; } 

Therefore, I recommend using char arrays whenever you use them as strings and char pointers whenever you use them as a data array.

C ++:

C ++ has its own string data type: std::string . If you just need the C-String version for std :: string (e.g. using some C-API), just use the c_str() member:

 std::string s = "Hello World"; your_c_call( s.c_str(), ... ); 

Unicode:

I want to have strings in Unicode, then you really have to go with something like

 char utf8String[] = u8"Hello World"; 

and try not to use wchar_t whenever possible. See this great article on this subject: http://www.nubaria.com/en/blog/?p=289 . Please, not that there is Unicode support for C ++. But as a rule, I am tempted to say that you should go with normal characters as much as you can. Interesting resource: http://www.cprogramming.com/tutorial/unicode.html

+1
source share

Is it C or C ++? In C ++ you should use std::string :

 std::string aString("stringcontenthere"); 
0
source share

All Articles