Can someone explain why the double quotes give me error here?

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]="

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    51

    Can someone explain why the double quotes give me error here?

    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    thank you

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Why not use std::string?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    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

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anybody give me an example on a double pointer array?
    By torquemada in forum C Programming
    Replies: 4
    Last Post: 10-22-2011, 05:25 AM
  2. Replies: 4
    Last Post: 10-31-2009, 07:18 PM
  3. single vs double quotes
    By innuendo in forum C++ Programming
    Replies: 17
    Last Post: 11-03-2008, 04:38 PM
  4. double quotes or carrots?
    By CSoFun in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2003, 04:05 PM
  5. outputing quotes(') and double quotes(")
    By lostboy in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-26-2002, 05:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21