string-literals

How should character arrays be used as strings?

大兔子大兔子 提交于 2019-12-29 07:35:13
问题 I understand that strings in C are just character arrays. So I tried the following code, but it gives strange results, such as garbage output or program crashes: #include <stdio.h> int main (void) { char str [5] = "hello"; puts(str); } Why doesn't this work? It compiles cleanly with gcc -std=c17 -pedantic-errors -Wall -Wextra . Note: This post is meant to be used as a canonical FAQ for problems stemming from a failure to allocate room for a NUL terminator when declaring a string. 回答1: A C

Perl replace multiple strings simultaneously

会有一股神秘感。 提交于 2019-12-29 06:44:09
问题 Is there any way to replace multiple strings in a string? For example, I have the string hello world what a lovely day and I want to replace what and lovely with something else.. $sentence = "hello world what a lovely day"; @list = ("what", "lovely"); # strings to replace @replist = ("its", "bad"); # strings to replace with ($val = $sentence) =~ "tr/@list/@replist/d"; print "$val\n"; # should print "hello world its a bad day".. Any ideas why it's not working? Thanks. 回答1: First of all, tr

String literals: pointer vs. char array

徘徊边缘 提交于 2019-12-28 03:38:50
问题 In this statement: char *a = "string1" What exactly is string literal? Is it string1 ? Because this thread What is the type of string literals in C and C++? says something different. Up to my knowledge int main() { char *a = "string1"; //is a string- literals allocated memory in read-only section. char b[] = "string2"; //is a array char where memory will be allocated in stack. a[0] = 'X'; //Not allowed. It is an undefined Behaviour. For me, it Seg Faults. b[0] = 'Y'; //Valid. return 0; }

String literals not allowed as non type template parameters

房东的猫 提交于 2019-12-28 01:52:11
问题 The following quote is from C++ Templates by Addison Wesley . Could someone please help me understand in plain English/layman's terms its gist? Because string literals are objects with internal linkage (two string literals with the same value but in different modules are different objects), you can't use them as template arguments either: 回答1: Your compiler ultimately operates on things called translation units, informally called source files. Within these translation units, you identify

Best way to convert string to array of object in javascript?

半城伤御伤魂 提交于 2019-12-27 14:42:25
问题 I want to convert below string to an array in javascript. {a:12, b:c, foo:bar} How do I convert this string into array of objects? Any cool idea? 回答1: I think that the best way of doing this, as Douglas Crockford (one of the biggests gurus of JavaScript) suggests in here is using the JSON native parser, as it is not only faster than the eval(), it's also more secure. Native JSON parser is already available in: Firefox 3.5+ IE 8+ Opera 10.5+ Safari Safari 4.0.3+ Chrome (don't know which

In which situation stringLit in StandardTokenParsers doesn't work?

本小妞迷上赌 提交于 2019-12-25 04:43:31
问题 I am writing a parser in which an arithmetic operation is going to be parsed. this arithmetic operation contains variables as well for instance: var1+1 The parser is as follow: package org.pvamu.hadoop.image; import org.apache.spark.serializer.{KryoSerializer, KryoRegistrator} import java.nio.file.Files; import java.io.File; import scala.util.parsing.combinator.lexical._ import scala.util.parsing.combinator.token.StdTokens import scala.util.parsing.combinator.lexical.StdLexical import scala

How can I align a string literal to an address which is multiple of 4?

核能气质少年 提交于 2019-12-24 14:59:19
问题 I'd like to ensure that a given string literal ends up at an address that is a multiple of 2, or even better, 4. Is there any way to achieve that, preferably without using any compiler-specific extensions? Or is it impossible? I'd like to do this so the lowermost bits of the string address are 0 and can be (ab)used as tag bits. 回答1: In C99 you can do this using a union, for example #define ALIGNED_STR_UNION(N, S) const union { char s[sizeof S]; int align; } N = { S } ALIGNED_STR_UNION(sssu,

Why is using string literals in Android discouraged? What other/better alternatives are available?

女生的网名这么多〃 提交于 2019-12-24 14:46:58
问题 I know that using string literals in Android is discouraged but I do not know why. I also would like to know what possible ways can get round this problem. 回答1: It's not necessarily a problem, just a warning. Android encourages the use of the strings resource file where possible but you certainly can hard-code strings and have it work correctly. See this question/answers: what's wrong with hardcoded string in android xml file? 回答2: This isn't an Android-specific issue. It is a "best practice"

Capture variable string in a regular expression?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 10:35:50
问题 In C#, I'm attempting to use the following capture pattern with a variable - I wonder if I'm going about this wrong. info_name is a string variable I pass to the method. Regex g = new Regex(@"""" + info_name + """>.+</span>"); // capture "info">Capture pattern</span> But it gives me an error, ')' expected about halfway through. This gives no error: Regex g = new Regex(@"""" + info_name +">.+</span>"); //^ 1 quote, not 3 I can't use this as a solution, I need to capture the " just before the

Appending character arrays using strcat does not work

戏子无情 提交于 2019-12-24 02:18:31
问题 Can some one tell me what's wrong with this code??? char sms[] = "gr8"; strcat (sms, " & :)"); 回答1: sms is an array of size 4 1 . And you're appending more char literals, which is going outside of the array, as the array can accommodate at max 4 chars which is already occupied by g, r, 8, \0 . 1. By the way, why exactly 4? Answer : Because that there is a null character at the end! If you mention the size of array as shown below, then your code is valid and well-defined. char sms[10] = "gr8";