Archived

This topic is now archived and is closed to further replies.

Gaiiden

What's with "\" ???

Recommended Posts

Gaiiden    5710
Why does C++ not like it when I do this: LPSTR path = "C:\"; ??? I get errors like "newline in constant" and "missing ;" on the next line down. When I take out the "\" in the string, I get no errors! Why is this? I've never had this problem before. I also noticed when putting it inot a define like this: #define PATH "C:\PFiles\C++\" // these comments aren't green!! the comments afterwards aren't comments! anyone know what's going on here? ============================== "Need more eeenput..." - #5, "Short Circuit" ============================== Edited by - Gaiiden on 10/29/00 2:34:30 PM

Share this post


Link to post
Share on other sites
Ironblayde    130
In C/C++, using a \ in a string indicates an escape sequence. For example:

\n - newline
\t - tab
\" - double-quotes

To get a backslash character, just use it twice in a row, like this: \\. So a valid filename would look like this:

#define PATH "c:\\pfiles\\C++\\"

-Ironblayde
 Aeon Software

The following sentence is true.
The preceding sentence is false.


Edited by - Ironblayde on October 29, 2000 3:42:29 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
\ starts a sequence ( in c/c++ ) ( like \n \r \t \" ).

If you want to use only a \ you have to write \\ ( so it looks like this

LPSTR path = "C:\\";

)

That''s all.

Share this post


Link to post
Share on other sites
Gorg    248
C was designed on a unix system, so you can use / instead of \\. \\ will not work if you are on a unix system only on windows. new compilers should be clever enough to make the switch, be I would not bet on it.

char* pcString[Size] = "c:/directory/file";

Share this post


Link to post
Share on other sites
Muzzafarath    146
quote:

the backslash is basically only used in the shell.



It's used in many more places than that, most Windows programs use \ (Explorer for example).

Edited by - Muzzafarath on October 30, 2000 8:14:44 AM

Share this post


Link to post
Share on other sites
furby100    102
Backslash is used under DOS, in case you have not been using computers for long enough to have used it, and the forward slash is not a valid delineation character for DOS. And the / being used in C is to do with the language. If you try using that under DOS it will _NOT_ work. The reason the www uses / as its delineation character is because it is based on UNIX, which as mentioned before used / as its delineation character.



Please state the nature of the debugging emergency.


sharewaregames.20m.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
So to sum up, in C/C++ if you want a forward slash in a string, use /.
If you want a backslash in a string, use \\.




Share this post


Link to post
Share on other sites
furby100    102
quote:
Original post by JonStelly
\0 = NULL



Technically, \0 is not always the same as NULL. NULL is the value of a pointer which does not point to any memory. \0 is the value of a character which is not defined. They do not have to be the same, although they are on DOS based Intel compatible platforms.



Please state the nature of the debugging emergency.


sharewaregames.20m.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by furby100

[quote]Original post by JonStelly
\0 = NULL



Technically, \0 is not always the same as NULL. NULL is the value of a pointer which does not point to any memory. \0 is the value of a character which is not defined. They do not have to be the same, although they are on DOS based Intel compatible platforms.



"\0" is not the value of a character which is undefined. "\0" represents the "null character", which has the ASCII code ''0'' (zero). The reason NULL==''\0'' is this: ''\0'' represents a byte (unsigned char) with value 0. NULL represents a pointer (stored as a long) with no value, which is also stored as 0. Hence, 0==0...

"Technically", they''re not the same because one represents a long and one represents an unsigned char, but they have the same value.

Of course, ''0'' has a value of 48 (0x30), it''s ASCII code.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Time to clear some confusion about NULL! According to the ISO C++ standard, NULL is defined as integer 0, but may not be represented in memory as 0. Therefore...

void* a = 0; // a does not point to anything
void* b = NULL; // a == b
void* c = 1; // error (can only convert integer 0 to ptr!)

However... Assigning zero to a pointer merely says you want a pointer that points to nothing. That doesn''t mean that the pointer is represented in memory by a 0. Therefore, the following two statements are not equivalent. (assuming ptr is a void*)

ptr = 0;
memset(&ptr, 0, sizeof(ptr));

Granted, I do not know of a platform where NULL pointers are not represented as 0 (actually, I think NeoTuri in DALnet #programmers said some RISC platform used -1 (0xFFFFFFFF), but I can''t verify that). According to the C++ standard though, it can happen.

AegisKnight
http://www.nerv-un.net/~aegis/

Share this post


Link to post
Share on other sites