literals

Why it is possible to assign string to character pointer in C but not an integer value to an integer pointer

旧巷老猫 提交于 2019-12-01 08:11:32
why in the below code int *p = 22 will give compile time error and ptr will print the value successfully . int main() { /*taking a character pointer and assigning a string to it*/ char *ptr = "Stackoverflow" ; //correct /*taking a int pointer and assigning a string to it*/ int *p = 22 ; //incorrect printf("%s",ptr); // correct and print printf("%d",p); //incorrect and give compile time error. return 0; } If you have a character array as for example char s[] = "Stackoverflow"; then the array designator used in expressions it is converted to pointer to its first element. So you may write char

Difference between addition of String Literals and String objects

痞子三分冷 提交于 2019-12-01 06:58:04
问题 What is the difference between an addition of String Literal and String Object? For example String s1 ="hello"; String s2 ="hello1"; String s3 ="hello" + "hello1"; String s4 ="hellohello1"; String s5 = s1 + s2; System.out.println(s3 == s4); // returns true System.out.println(s3 == s5); // return false System.out.println(s4 == s5); // return false Why do s3 / s4 not point to the same location as s5 ? 回答1: Because s1 + s2 is not a constant expression , since s1 and s2 are not final , therefore

Does java optimize string literal toLowerCase()?

一曲冷凌霜 提交于 2019-12-01 06:45:54
Does java optimize operations with string literals? For example, does "literal".toLowerCase() always create a new string instance? toLowerCase() calls toLowerCase(Locale.getDefault()) . Looking at the implementation you'll see that the original String is returned if no characters need to be changed: public String toLowerCase(Locale locale) { if (locale == null) { throw new NullPointerException(); } int firstUpper; final int len = value.length; /* Now check if there are any characters that need to be changed. */ scan: { for (firstUpper = 0 ; firstUpper < len; ) { char c = value[firstUpper]; if

Does java optimize string literal toLowerCase()?

青春壹個敷衍的年華 提交于 2019-12-01 05:03:20
问题 Does java optimize operations with string literals? For example, does "literal".toLowerCase() always create a new string instance? 回答1: toLowerCase() calls toLowerCase(Locale.getDefault()) . Looking at the implementation you'll see that the original String is returned if no characters need to be changed: public String toLowerCase(Locale locale) { if (locale == null) { throw new NullPointerException(); } int firstUpper; final int len = value.length; /* Now check if there are any characters

Java Character literals value with getNumericValue()

六眼飞鱼酱① 提交于 2019-12-01 04:11:12
问题 Why do I get the same results for both upper- and lowercase literals? For instance: char ch1 = 'A'; char ch2 = 'a'; char ch3 = 'Z'; char ch4 = 'z'; print("ch1 -- > " + Integer.toBinaryString(Character.getNumericValue(ch1))); print("ch2 -- > " + Integer.toBinaryString(Character.getNumericValue(ch2))); print("ch3 -- > " + Integer.toBinaryString(Character.getNumericValue(ch3))); print("ch4 -- > " + Integer.toBinaryString(Character.getNumericValue(ch4))); As results I get: ch1 -- > 1010 ch2 -- >

Is it possible to have a pointer literal?

我们两清 提交于 2019-12-01 03:55:18
In C one can have string literals in the form of char *string = "string here"; integer literals: uint8_t num = 5; long literals: long long bigNum = 90322L; floating point literals: float decimal = 6.3f; Is the a way to have a pointer literal? That is a literal address to a memory space. I am doing some work on an embedded project and need to hard code a value for a DMA access. I am doing something similar to the following: uint32_t *source = 0x08000000; While this compiles and works correctly I get the following compiler error (I'm using a variant of GCC ): cc0144: {D} warning: a value of type

Is '\\u0B95' a multicharacter literal?

非 Y 不嫁゛ 提交于 2019-12-01 03:38:13
In a previous answer I gave , I responded to the following warning being caused by the fact that '\u0B95' requires three bytes and so is a multicharacter literal : warning: multi-character character constant [-Wmultichar] But actually, I don't think I'm right and I don't think gcc is either. The standard states: An ordinary character literal that contains more than one c-char is a multicharacter literal . One production rule for c-char is a universal-character-name (i.e. \uXXXX or \UXXXXXXXX ). Since \u0B95 is a single c-char , this is not a multicharacter literal. But now it gets messy. The

in CoffeeScript, how can I use a variable as a key in a hash?

微笑、不失礼 提交于 2019-12-01 02:03:35
eg: So: foo = "asdf" {foo: "bar"} eval foo # how do I get {"asdf": "bar"} ? # this will throw parse error: {(eval foo): "bar"} This is a simple syntax question: how do I get CoffeeScript to construct a hash dynamically, rather than doing it by hand? For anyone that finds this question in the future, as of CoffeeScript 1.9.1 interpolated object literal keys are supported! The syntax looks like this: myObject = a: 1 "#{ 1 + 2 }": 3 See https://github.com/jashkenas/coffeescript/commit/76c076db555c9ac7c325c3b285cd74644a9bf0d2 Why are you using eval at all? You can do it exactly the same way you'd

Are hard-coded STRINGS ever acceptable?

痴心易碎 提交于 2019-12-01 00:40:47
Similar to Is hard-coding literals ever acceptable? , but I'm specifically thinking of "magic strings" here. On a large project, we have a table of configuration options like these: Name Value ---- ----- FOO_ENABLED Y BAR_ENABLED N ... (Hundreds of them). The common practice is to call a generic function to test an option like this: if (config_options.value('FOO_ENABLED') == 'Y') ... (Of course, this same option may need to be checked in many places in the system code.) When adding a new option, I was considering adding a function to hide the "magic string" like this: if (config_options.foo

Is it possible to have a pointer literal?

老子叫甜甜 提交于 2019-12-01 00:01:00
问题 In C one can have string literals in the form of char *string = "string here"; integer literals: uint8_t num = 5; long literals: long long bigNum = 90322L; floating point literals: float decimal = 6.3f; Is the a way to have a pointer literal? That is a literal address to a memory space. I am doing some work on an embedded project and need to hard code a value for a DMA access. I am doing something similar to the following: uint32_t *source = 0x08000000; While this compiles and works correctly