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; } }
This is a discussion on Can someone explain why the double quotes give me error here? within the C++ Programming forums, part of the General Programming Boards category; Code: #include "dot.h" namespace dot { const char* leader(char* s){ for (int i=0;i<ML;i++) s[i]="."; s[ML]="
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
Why not use std::string?
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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.
I used to be an adventurer like you... then I took an arrow to the knee.
Well, to be pedantic, the type is actually const char[n] where n is the length of the array (the length of the string + 1).
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^