-1

Why does this:

const char cwords = "These are even more words";

result in an **error**: cannot initialize a variable of type 'const char' with an lvalue of type 'const char [22]'

but this:

const char * cwordsp = "These are more words"; Work? (Not result in an error)

It seems like the pointer cwordsp should need to point to the memory address of that c string. Am I wrong?

Startec
  • 12,496
  • 23
  • 93
  • 160

3 Answers3

2

A C-string is nothing more than an array of characters.

So in addition to your working example, you could also do something like this:

const char cString[] = "Hello world";

Which is basically equivalent to this:

const char cString[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' };

Note that this is an array of chars, not a single char.

The reason that you run into problems with this:

const char cString = "Hello world";

is because "Hello world"; can't possibly be interpreted as a char. A char type expects just a single character. Like this:

const char c = 'h';
nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • So with the use of the pointer `const char * cwordsp = "These are more words";` `These are more words` returns a pointer to an array of `chars`? – Startec Jun 28 '15 at 21:56
  • Effectively, yes. It returns the memory address of an array of characters. – nhgrif Jun 28 '15 at 21:57
  • No need for the address of / reference operator? And why doesn't the pointer need to point to a `char[]`? – Startec Jun 28 '15 at 21:57
  • No. You're probably better off thinking of it as an array of characters. That's really all it is. Are you familiar with arrays (say, an array of `int`)? – nhgrif Jun 28 '15 at 21:58
  • yes, I am familiar with arrays, however it is this pointer to the array that is confusing. If I have a single `char` I would declare it as `char * cp = & (aChar variable)` So why with an array don't I need to specify that is is a pointer to a char array? – Startec Jun 28 '15 at 22:02
  • 1
    It's not a pointer to an array. An array *is* a pointer to a contiguous block of memory which holds the values at the various indexes. – nhgrif Jun 28 '15 at 22:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81808/discussion-between-nhgrif-and-startec). – nhgrif Jun 28 '15 at 22:05
0

In const char cwords = "These are even more words"; you are initializing an 8-bit integer (const char) with a pointer to const char. This is an illegal conversion.

limits
  • 295
  • 4
  • 12
0

Because char is 1 byte.

You are trying to put more than 1 byte in to a datatype which holds 1 byte. const char cwords = "These are even more words";

In this example you create an array and get a pointer pointing to the first char in the array const char * cwordsp = "These are more words";

A pointer array and a regular array is almost the same thing, but only almost. You can read more about that: C/C++ int[] vs int* (pointers vs. array notation). What is the difference?

Community
  • 1
  • 1