c-strings

Expand macros inside quoted string [duplicate]

房东的猫 提交于 2019-11-30 14:31:07
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C Macros to create strings I have a function which accepts one argument of type char* , like f("string"); If the string argument is defined by-the-fly in the function call, how can macros be expanded within the string body? For example: #define COLOR #00ff00 f("abc COLOR"); would be equivalent to f("abc #00ff00"); but instead the expansion is not performed, and the function receives literally abc COLOR . In

Expand macros inside quoted string [duplicate]

妖精的绣舞 提交于 2019-11-30 10:54:27
Possible Duplicate: C Macros to create strings I have a function which accepts one argument of type char* , like f("string"); If the string argument is defined by-the-fly in the function call, how can macros be expanded within the string body? For example: #define COLOR #00ff00 f("abc COLOR"); would be equivalent to f("abc #00ff00"); but instead the expansion is not performed, and the function receives literally abc COLOR . In particular, I need to expand the macro to exactly \"#00ff00\" , so that this quoted token is concatenated with the rest of the string argument passed to f() , quotes

Set Qt default encoding to UTF-8

时光怂恿深爱的人放手 提交于 2019-11-30 08:25:10
In my Qt application my source code files are encoded as UTF-8. For the following code... QMessageBox::critical(this, "Nepoznata pogreška", "Dogodila se nepoznata pogreška! Želite li zatvoriti ovaj program ?", QMessageBox::Yes, QMessageBox::No); ...when I show that message box, the character "š" wouldn't be displayed as "š", but as something strange. This is because Qt converts all C-strings as if they are encoded using LATIN-1. To solve this I've been using: QMessageBox::critical(this, QString::fromUtf8("Nepoznata pogreška"), QString::fromUtf8("Dogodila se nepoznata pogreška! Želite li

What happens to memory after '\\0' in a C string?

空扰寡人 提交于 2019-11-29 20:23:19
Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the user of my function a C-string, whose length I do not know at the beginning of the function. I can place only an upper bound on the length at the outset, and, depending on processing, the size may shrink. The question is, is there anything wrong with allocating enough heap space (the upper bound) and then terminating the string well short of that during processing? i.e. If I stick a '\0' into the middle of the allocated memory, does (a.) free() still work properly, and (b.) does the space after the '\0'

Are strtol, strtod unsafe?

£可爱£侵袭症+ 提交于 2019-11-29 13:28:56
It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include <stdlib.h> #include <stdio.h> int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // segmentation fault return 0; } Above, I did not perform any casts myself. However, strtol() basically cast my const char * into a char * for me, without any warnings or anything. (In fact, it wouldn't allow you to type bar as a const char * , and so forces the unsafe change

Set Qt default encoding to UTF-8

爷,独闯天下 提交于 2019-11-29 11:34:20
问题 In my Qt application my source code files are encoded as UTF-8. For the following code... QMessageBox::critical(this, "Nepoznata pogreška", "Dogodila se nepoznata pogreška! Želite li zatvoriti ovaj program ?", QMessageBox::Yes, QMessageBox::No); ...when I show that message box, the character "š" wouldn't be displayed as "š", but as something strange. This is because Qt converts all C-strings as if they are encoded using LATIN-1. To solve this I've been using: QMessageBox::critical(this,

Why should one use std::string over c-style strings in C++?

喜夏-厌秋 提交于 2019-11-29 03:50:18
"One should always use std::string over c-style strings( char * )" is advice that comes up for almost every source code posted here. While the advice is no doubt good, the actual questions being addressed do not permit to elaborate on the why? aspect of the advice in detail. This question is to serve as a placeholder for the same. A good answer should cover the following aspects(in detail): Why should one use std::string over c-style strings in C++? What are the disadvantages (if any) of the practice mentioned in #1 ? What are the scenarios where the opposite of the advice mentioned in #1 is a

What happens to memory after '\0' in a C string?

这一生的挚爱 提交于 2019-11-28 16:25:07
问题 Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the user of my function a C-string, whose length I do not know at the beginning of the function. I can place only an upper bound on the length at the outset, and, depending on processing, the size may shrink. The question is, is there anything wrong with allocating enough heap space (the upper bound) and then terminating the string well short of that during processing? i.e. If I stick a '\0' into the

Access violation writing location when working with pointers to char

送分小仙女□ 提交于 2019-11-28 13:46:20
I am writing a very simple program that removes duplicate chars from a string. I ran it visual studio and got the error: Unhandled exception at 0x00d110d9 in inteviews.exe: 0xC0000005: Access violation writing location 0x00d27830. I really don't see what the problem is. current cell gets the value of the next cell. void remove(char *str, char a) { while (*str != '\0') { if (*(str+1) == a) { remove(str + 1, a); } *str = *(str +1 );//HERE I GET THE ERROR ++str; } } int _tmain(int argc, _TCHAR* argv[]) { char *str = "abcad"; while (*str != '\0') { remove(str,*str); str++; } std::cout << str <<

Why Doesn't string::data() Provide a Mutable char*?

拜拜、爱过 提交于 2019-11-28 13:17:21
In c++11 array , string , and vector all got the data method which: Returns pointer to the underlying array serving as element storage. The pointer is such that range [ data() ; data() + size() ) is always a valid range, even if the container is empty. [ Source ] This method is provided in a mutable and const version for all applicable containers, for example: T* vector<T>::data(); const T* vector<T>::data() const; All applicable containers, that is, except string which only provides the const version : const char* string::data() const; What happened here? Why did string get shortchanged, when