Q3.Problem

Q.3. Why does decleration of char with out pointer
datatype causes error for example

1
2
  const char suitNames[] = {"Clubs ", "Diamonds ", "Spades ", "Clubs "};//causes error
const char* suitNames[] = {"Clubs ", "Diamonds ", "Spades ", "Clubs "};//doesnt cause error 
The first creates an array of constant characters. "Clubs", etc are not const char, they are const char*s.
Yes but why isnt the first const char for example "Clubs" is intialized why should there be need for pointer storing and int storing will cause an error
const char suitNames[4]; is an array of four characters. Each element in the array is a const char. char is a datatype that holds one character. One byte.

"Diamonds " is not a single charater. It is an array or characters. If you need an array of arrays, then an array of single characters just doesn't suit you.
Last edited on
We use ' ' for chars
And " " for strings
BTW chars is for characters not words
Yes you made the array point clear for me but i understand the use for pointer in it now it's been made clear by you to me that const char suitNames[] is reqired but why this will cause error instead we should write const char * suitNames[]
Lets forget the array. Lets take just one value. You would like to write const char name = "fubar"; but that does not work. Why?

You are attempting to store the address of a constant string literal (which are stored in memory space of their own) into a variable that does store one character. An address of a memory location is not same as a char.

You are attempting to store the address of a constant string literal (which are stored in memory space of their own) into a variable that does store one character. An address of a memory location is not same as a char.

Did you mean char has a memory of 1 byte and it will not store const char vaariable but it will do so if its a const char pointer variable
No.

A char variable stores one character.
A pointer variable stores one pointer.
Thank you its clear now :)
Topic archived. No new replies allowed.