Questions tagged [string-literals]

String literals concern the syntactic representation of literal constant strings in C, C++ and others.

C and C++ have several ways to represent strings in code. They include the basic double quoted "Hello, World!". Strings can be concatenated "as" " follows". C++-0X has added new encodings u"Hello" for UTF16 (char16_t), U"world" for UTF32 (char32_t) is addition to L"long" for wchar_t. There are rules for concatenating different encodings.

1193 questions
-3
votes
1 answer

extracting string literals Delphi

Please give me an idea how to extract all string literals from Delphi file. There is no problem with literals surrounded by quotes. But string literals also can be presented as hash-codes. Also it can consist of parts in quote and hash-codes…
GolovDanil
  • 133
  • 2
  • 11
-3
votes
3 answers

Assigning an element of a pointer to another element from the same pointer

I'm trying to assign an element from a pointer the same value as another element from the same pointer. int testFunc() { char *p = "123"; p[0] = p[2]; return 0; } Curious as to why the above code doesn't work - and what is the best way…
Phil O'kelly
  • 161
  • 1
  • 3
  • 14
-3
votes
1 answer

Segmentation fault in this C program

This is a program to copy string1 to string2 from K&R book. #include void strcpy_m(char *t1, char *t2); int main() { char *s1 = "this is 1st"; char *s2 = "this is second"; strcpy_m(s1, s2); printf("%s\t%s\n",s1, s2); …
mrigendra
  • 1,472
  • 3
  • 19
  • 33
-3
votes
2 answers

Why modifying a string with different ways have different behaviors?

Why these two code snippets have different behaviors char p[] = "hello"; p[0] = 'W'; //works fine While this gives a segmentation fault char *ptr = "hello"; ptr[0] = 'W'; // undefined behavior What is the difference between both codes,why i am…
Rajeev Singh
  • 3,292
  • 2
  • 19
  • 30
-3
votes
2 answers

How do I free up the memory consumed by a string literal?

How do I free up all the memory used by a char* after it's no longer useful? I have some struct struct information { /* code */ char * fileName; } I'm obviously going to save a file name in that char*, but after using it some time afterwards, I…
YoTengoUnLCD
  • 600
  • 7
  • 15
-4
votes
2 answers

Why when i add \ to cout it dosent display it on output

When I try to do printf("----/");, the \ is removed from the output #include int main() { std::cout << ("Welcome to the game\n\n"); printf("--------\n"); printf("----O---\n"); printf("---/|\--\n"); …
-4
votes
2 answers

Getting a bus error trying to switch letters in a string

Below is a snippet of my code. My code is supposed to reverse alphabet chars only and skip any special characters. I am currently trying to use "ab-cd" and hope to print "ba-cd", but when I run my code I get a bus error. I have narrowed down the…
-4
votes
1 answer

How to substitute a variable in a template string?

A function which has 2 arguments: First argument will have string - 'hello ${val}' Second argument will be an object - {'val':'world'} I need to write a solution where the first argument will access the property of the second argument's object and…
-4
votes
2 answers

Type of strings

I got quite confused about what is what. Would you please tell me what each variables type is? char foo[] = "bar"; char *bar = nullptr; char const *qux = nullptr; Aditionally, what is the type of "bar"?
Swordfish
  • 12,971
  • 3
  • 21
  • 43
-4
votes
1 answer

compiler does not allow me to edit the passed string

I looked around and I could not find a solution of my problems in other question. For some reason, then I get segmentation fault when I run my program and it seems to be because i am changing the give string. I tried passing a pointer to a char…
-4
votes
1 answer

Retain backslash in regex in 'gsub'

I want to replace spaces and forward slashes with an underscore, and retain backslashes. How do I do this? I have the following regex: "hello\h /123".gsub(/[\s+\/]/, "_") #=> "helloh__123" But it also replaces backslashes in the string.
-4
votes
2 answers

I am a beginner in programming in c,need help in sizeof() string constant?

/**** Program to find the sizeof string literal ****/ #include int main(void) { printf("%d\n",sizeof("a")); /***The string literal here consist of a character and null character, so in memory the ascii values of both the characters…
-4
votes
1 answer

string literal pointer issue passing to nested functions

So I have function that logs some data and writes that to a file and returns the path to the file as a string. I then need to take that string and pass it to a function that parses the file and places the data in a structure before the file can be…
skevthedev
  • 447
  • 1
  • 7
  • 20
-4
votes
3 answers

Ways to distinguish string literal and runtime generated string

Suppose I have a function that takes a string as input: SomeOutputType f_impl(const char* s); Most call sites just use string literals as input, e.g. f("Hello, world"). Suppose I have implemented the following function to compute the result at…
Kan Li
  • 8,557
  • 8
  • 53
  • 93
-5
votes
1 answer

How to use string literals without using arrays and #define preprocessor directive?

I am trying to implement a command line interface function for a micro-controller, which has very limited stack size (around 1kB) and I do not want to use an array to hold the string values to not push them into the stack. I come up with two…
albin
  • 773
  • 1
  • 8
  • 27
1 2 3
79
80