-3

For the sake of efficiency, is there ever a reason to use one of these over the other?

char str1[] = "Hello"
char *str2  = "World"
Marcus McLean
  • 1,306
  • 2
  • 13
  • 24

1 Answers1

4

They do two different things.

char str1[] = "Hello";

is equivalent to

char str1[6] = "Hello";

It makes str1 an array of 6 chars, initialized to "Hello" plus a '\0' terminator.

This:

char *str2  = "World";

makes str2 a pointer to char, pointing to the first element of a statically allocated read-only array of 6 chars, which is initialized to "World" plus a '\0' terminator.

I mentioned that the array is read-only -- but for historical reasons it's not actually const; attempting to change it has undefined behavior. You should define it as const so the compiler will warn you if you incorrectly attempt to modify it:

const char *str2 = "World";

str1 and str2 can be used interchangeably in some, but not all, contexts. For example, either puts(str1) or puts(str2) will print the corresponding string to standard output (followed by a newline). This is because an array name, in most contexts, "decays" to a pointer to the array's first element.

Since str1 is an array, you can apply sizeof to it to determine how big the array is; sizeof str2 gives you the size of a char* pointer, which probably won't be useful.

With the first declaration, you can modify the characters of the array, but you can't change its size, and you can't make str1 refer to a different array.

With the second declaration, you can't modify the characters of the array, but you can change the pointer value itself so it points to some other array, or to nothing if you do str2 = NULL;.

If all you want to do with str1 or str2 is pass it to something that expects a char* that points to a string, you can use either form. The pointer form makes it a bit clearer that you're not going to be modifying the string. You can even write:

const char *const str2 = "World";

if you want to prevent modifying either the string or the pointer.

If you want to do something else, one form or the other may have some advantages, but I can't comment further without knowing what you're using it for.

There shouldn't be any significant difference in efficiency. Concentrate first on what how you want your code to behave. Adding const to anything you don't intend to modify helps document your intent and may help the compiler generate better code (because it doesn't need to assume that something may have been modified).

Recommended reading: the comp.lang.c FAQ, particularly section 6 which covers arrays and pointers.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thank you. Perfect answer to my question. I am trying to pass a string to multiple functions that could potentially change the contents of the string. The size can remain constant, it doesn't matter. – Marcus McLean Apr 27 '13 at 21:16
  • 1
    In that case efficiency is irrelevant. If you need to modify the string, then you can't use a literal. – David Heffernan Apr 27 '13 at 21:40