11

I'm currently using

char *thisvar = "stringcontenthere";

to declare a string in C.

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

And how about generating a C-String from C++-Strings?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Daniel
  • 3,017
  • 12
  • 44
  • 61
  • 2
    Note that this should be `const char *thisvar = "stringcontenthere";` (additional `const`). There is an implicit `const`-removing conversion from a string literal to `char*`, but that's deprecated and should not be used. – sbi Jun 14 '10 at 10:32
  • 2
    Also note that `char* foo;` __defines__ a variable (of type `char*`) and `char* foo="bar";` __defines and initializes__ a variable. A __declaration__ would be `extern char* foo;`. (See http://stackoverflow.com/questions/1410563/1410632#1410632 for what's a definition and what's a declaration.) – sbi Jun 14 '10 at 10:39
  • 1
    You should clarify whether or not you are interested in C++ as well, as the C++ tag does not find any notice in the question nor the subject. – math Sep 30 '13 at 09:00

5 Answers5

16

In C it depends on how you'll use the string:

  • named constant: your char* str = "string"; method is ok (but should be char const*)
  • data to be passed to subfunction, but will not not used after the calling function returns:
    char str[] = "string";
  • data that will be used after the function it is declared in exits: char* str = strdup("string");, and make sure it gets freed eventually.

if this doesnt cover it, try adding more detail to your answer.

David X
  • 3,998
  • 3
  • 29
  • 32
1

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

C:

As David pointed out it depends on how to use the string in C:

  • as a constant then: const char s[] = "Hello World";
  • as a string containing variable data then: char s[] = "Hello World";
  • as a data array char *data; Initialization then should be customized.

Please note in C there are all Strings Null-terminated, that means the definition of e.g. char s[] = "foo"; implicitly includes a NULL character at the end s[3]='\0'.

Also please note the subtile difference between char *s and char s[] which might often behave the same but sometimes not! (see Is an array name 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;
}

So I recommend to use char arrays whenever you use them as strings and char pointers whenever you use them as data array.

C++:

In C++ there is an own string data type: std::string. If you just need to have a C-String version of a 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 you want to have unicode strings then you should really go with something like

char utf8String[] = u8"Hello World";

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

Community
  • 1
  • 1
math
  • 8,514
  • 10
  • 53
  • 61
1
const char *thisvar="stringcontenthere";
KMån
  • 9,896
  • 2
  • 31
  • 41
1

As other suggested, and I you want to "do it" the C++ way, use a std::string.

If you somehow need a C-string, std::string has a method that gives a 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;
}
ereOn
  • 53,676
  • 39
  • 161
  • 238
0

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

std::string aString("stringcontenthere");
Péter Török
  • 114,404
  • 31
  • 268
  • 329