string-literals

Understanding C-strings & string literals in C++

限于喜欢 提交于 2020-01-04 04:34:08
问题 I have a few questions I would like to ask about string literals and C-strings. So if I have something like this: char cstr[] = "c-string"; As I understand it, the string literal is created in memory with a terminating null byte, say for example starting at address 0xA0 and ending at 0xA9, and from there the address is returned and/or casted to type char [ ] which then points to the address. It is then legal to perform this: for (int i = 0; i < (sizeof(array)/sizeof(char)); ++i) cstr[i] = 97

How long does a string constant live in c++?

柔情痞子 提交于 2020-01-03 14:16:21
问题 I've been wondering, how long does a string constant live in C++. For example, if I create some const char *str = "something" inside a function, would it be safe to return the value of str? I wrote a sample program and was really surprised to see that such returned value still stored that string. Here is the code: #include <iostream> using namespace std; const char *func1() { const char *c = "I am a string too"; return c; } void func2(const char *c = "I'm a default string") { cout << c <<

String literals: Where do they go?

雨燕双飞 提交于 2020-01-02 20:03:34
问题 I am interested in where string literals get allocated/stored. I did find one intriguing answer here, saying: Defining a string inline actually embeds the data in the program itself and cannot be changed (some compilers allow this by a smart trick, don't bother). But, it had to do with C++, not to mention that it says not to bother. I am bothering. =D So my question is where and how is my string literal kept? Why should I not try to alter it? Does the implementation vary by platform? Does

C++ string literal equality check?

拜拜、爱过 提交于 2020-01-02 06:09:20
问题 == is not how we compare two arrays, since that would only compare the addresses: #include <iostream> int main() { char a[] = "aaa"; char b[] = "aaa"; if (a == b) std::cout << "Yes" << std::endl; else std::cout << "No" << std::endl; return 0; } This code even gives me a warning: Array comparison always evaluates to false But when I tried this: if ("aaa" == "aaa") It seemed to be working fine. Still gives me a warning, but the warning is: Condition is always true At first, I thought it was

Restricting string literals to Text only

拜拜、爱过 提交于 2019-12-31 11:00:10
问题 I'm aware that the OverloadedStrings language pragma wraps an implicit fromString around all string literals. What I'd like to do is not actually overload strings, but merely change their meaning so that they are always turned into Text , and therefore, using a string literal as a list of characters should result in a type error. It appears to be impossible to import the IsString class without also importing the String instance for that class. Does ghc provide some way for me to restrict

When we can or cannot modify String Literals [duplicate]

我是研究僧i 提交于 2019-12-31 07:42:06
问题 This question already has answers here : Segmentation fault when modifying a string [duplicate] (2 answers) Closed 4 years ago . #include<stdio.h> int main () { char *s="FIGHT" ; printf("\n Whole string is %s ", s ); // Printing FIGHT -- this is fine s[0]='L' ; printf ("\n Now whole string is %s", s ); // Printing LIGHT -- My Question is how string literal constant is getting modified when it is being stored in read only memory . } Above Code is working fine on my system. 回答1: TL;DR -- Never.

Modifying string literal passed in as a function

时间秒杀一切 提交于 2019-12-31 05:29:06
问题 If I have a function in program int main(){ char *name = "New Holland"; modify(name); printf("%s\n",name); } that calls this function void modify(char *s){ char new_name[10] = "Australia"; s = new_name; /* How do I correct this? */ } how can I update the value of the string literal name (which now equals new Holland) with Australia. The problem I think that I face is the new_name is local storage, so after the function returns, the variable is not stored 回答1: Try this: #include <stdio.h> void

What is the purpose of the single quotes in this regex expression?

假如想象 提交于 2019-12-31 01:28:07
问题 I've inherited some C# code with the following regular expression Regex(@"^[a-zA-Z''-'\s]{1,40}$") I understand this string except for the role of the single quotes. I've searched all over but can't seem to find an explanation. Any ideas? 回答1: From what I can tell, the expression is redundant. It matches a-z or A-Z , or the ' character, or anything between ' and ' (which of course is only the ' character again, or any whitespace. I've tested this using RegexPal and it doesn't appear to match

Bus error troubleshooting

旧巷老猫 提交于 2019-12-30 03:18:22
问题 I am trying reverse a string. This is the code I tried: #include<stdio.h> #include<string.h> int main(){ char *c="I am a good boy"; printf("\n The input string is : %s\n",c); printf("\n The length of the string is : %d\n",strlen(c)); int i,j; char temp; int len=strlen(c); for(i=0,j=len-1;i<=j;i++,j--) { temp=c[i]; c[i]=c[j]; c[j]=temp; //printf("%c\t%c\n",*(c+i),*(c+(len-i-1))); } printf("\n reversed string is : %s\n\n",c); } The code outputs a Bus error : 10 . But if I rewrite the same code

How to write unicode cross symbol in Java?

心不动则不痛 提交于 2019-12-29 18:36:09
问题 I'm trying to write this unicode cross symbol (𐀵) in Java: class A { public static void main(String[] args) { System.out.println("\u2300"); System.out.println("\u10035"); } } I can write o with a line through it (⌀) just fine, but the cross symbol doesn't show up, instead it just prints the number 5: # javac A.java && java A ⌀ ဃ5 Why? 回答1: You're looking for U+10035, which is outside the Basic Multilingual Plane. That means you can't use \u to specify the value, as that only deals with U+0000