slash character in char array

Hi,
I have a silly question. I need to initialize a char array with something like

char myarray[2]={'','r'};

but I have some problem with this code since when I try to compile I have an error

Exception in thread "Thread=10" java.lang.StackOverflowError

I think that \ represent some special character in the IDE...how can I transform the slash to a "pure" character (not sure how to say it...)?

thanks!

char myarray[2]={'\\','r'};

Or did you mean simply '\r' ?

actually it should be

char myarray[2]={'\\','r'};

but also in this way the compiler gives me the same error :frowning:

The error comes from the preprocessor - usually the comment stripping routines.

That line as it stands does not cause an error - you should check the rest of your code, especially your comments.

Use the decimal value of the ASCII for the characters you wish:

char myarray[2]={92,114};

I am sure the error comes from the '\' because if I remove it I do not have compilation problems.

The decimals should work....but I guess that I can not make a composite array of characters and decimals like

char myarray[2]={92,'r'};

right?

how do I specify that the 92 is decimal or that myarray is coded with decimals?

I guess that I can not make a composite array of characters and decimals like

Why would you guess that? Try it and see.

Everything is a number. Internally stored as 8-bit values (0-255). Everything else is purely a human interpretation of those 8 bits, including 'r'.

I suggest you post your entire code so we can replicate the error. We cannot replicate the error from just that one line.

It actually worked with the mixed dec and char :slight_smile:

char mystring[2]={92,'r'};

Thanks guys that helped!!!

You know of course that what you have now is not nul-terminated, and cannot be used in str*() functions that accept a string.

You could also use:

char mystring[2] = "\\r";

KeithRB:
You know of course that what you have now is not nul-terminated, and cannot be used in str*() functions that accept a string.

You could also use:

char mystring[2] = "\\r";

... except the strn* functions that take a length parameter :stuck_out_tongue:

actually I can still create the array with the last character being the end of line:

char mystring[3]={92,'r','\n'};

and this works too :slight_smile:

Except you still have no terminating character (line feed isn't end of string - character 0 is):

char mystring[4]={92,'r','\n', 0};

While we are nitpicking, it might be germane to point out that the character '' is apparently most clearly called the backslash character, not the slash character, which is '/' .

AWOL:

char myarray[2]={'\\','r'};

Or did you mean simply '\r' ?

To me this is really good question.
That and is the array supposed to be a character array or a C string?

In other words:

  1. is this supposed to be a 2 character array or a C string?
  2. is this supposed to contain backslash and a r or a carriage return?

--- bill