It works with single quotes. I just want to know why double quotes dont work.Code:#include "dot.h" namespace dot { const char* leader(char* s){ for (int i=0;i<ML;i++) s[i]="."; s[ML]="\0"; return s; } }
It works with single quotes. I just want to know why double quotes dont work.Code:#include "dot.h" namespace dot { const char* leader(char* s){ for (int i=0;i<ML;i++) s[i]="."; s[ML]="\0"; return s; } }
Because single quotes indicate a char value. Double quotes are interpreted by the compiler as null terminated C strings, which cannot be assigned to a single char.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
thank you
Part of the job of a compiler is type-checking data types and making sure that you don't do something stupid like try to divide an apple by an orange... metaphorically speaking. In this case, the double-quoted string literal is likely of type const char* (the address of the string literal in read-only memory) which you are attempting to assign to a char. From the compiler's standpoint, it sees you doing something questionable and rightly complains about it. As mentioned by MK27, single-quoted values represent individual characters and it is completely acceptable to assign a char to a char.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens