system
June 9, 2014, 2:52pm
#1
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!
system
June 9, 2014, 2:59pm
#2
char myarray[2]={'\\','r'};
Or did you mean simply '\r' ?
system
June 9, 2014, 3:04pm
#3
actually it should be
char myarray[2]={'\\','r'};
but also in this way the compiler gives me the same error
system
June 9, 2014, 3:27pm
#4
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};
system
June 9, 2014, 3:38pm
#6
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.
system
June 9, 2014, 3:46pm
#8
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.
system
June 9, 2014, 5:28pm
#9
It actually worked with the mixed dec and char
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";
system
June 9, 2014, 5:34pm
#11
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
system
June 9, 2014, 5:49pm
#12
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
system
June 9, 2014, 5:51pm
#13
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:
is this supposed to be a 2 character array or a C string?
is this supposed to contain backslash and a r or a carriage return?
--- bill