c-strings

How to declare constexpr C string?

烂漫一生 提交于 2019-12-08 15:54:07
问题 I think i quite understand how to use the keyword constexpr for simple variable types, but i'm confused when it comes to pointers to values. I would like to declare a constexpr C string literal, which will behave like #define my_str "hello" That means the compiler inserts the C string literal into every place where i enter this symbol, and i will be able to get its length at compile-time with sizeof. Is it constexpr char * const my_str = "hello"; or const char * constexpr my_str = "hello"; or

Can I convert char*[20] to char[][20]?

血红的双手。 提交于 2019-12-08 14:24:16
问题 I've corrected the program by myself now. this is still the -Never- answered question: I have a 2D array of chars that will contain a word every array. I split a char* word by word with a function to place them in the array. My problem is that it doesn't print the word but random characters. May it be a problem of pointers? I'm not sure about the conversion of char*[20] to char[][20] because I want filter a char*spamArray[20] into a char[][20] I need to pass char*[20] to the filter which has

Does the cin function add null terminated at the end of input? [closed]

喜夏-厌秋 提交于 2019-12-08 14:04:56
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . If I declare a character array: char arr[200] and then I subsequently use the function cin to read values into arr[200] and I type into the command window line: abcd Is there a null terminated: \0 automatically

Reversing C-style format strings in Python (`%`)

扶醉桌前 提交于 2019-12-08 05:33:52
问题 Introduction and setup Suppose I have a 'template'* string of the form, >>> template = """My %(pet)s ate my %(object)s. ... This is a float: %(number)0.2f. ... %(integer)10d is an integer on a newline.""" With this template I can generate a new string with, >>> d = dict(pet='dog', object='homework', number=7.7375487, integer=743898,) >>> out_string = template % d >>> print(out_string) My dog ate my homework. This is a float: 7.74. 743898 is an integer on a newline. How nice! Question I'd like

How to correctly convert unsigned char to CString and once again, reversely convert to the result of CString to unsigned char?

纵然是瞬间 提交于 2019-12-07 18:19:38
问题 I have problems with Converting unsigned char to CString and reversely, converting the result of converted CString to unsigned char. first, I try to using CString.append();. /////////////////////////// CString result2; CString result3; unsigend char unchar_result[9]; unchar_result = {143, 116, 106, 224, 101, 104, 57, 157}; unchar_result[8] = NULL; for(int i=0; i<8; i++){ result2=(char)temp[i]; result3.append(result2); } ////////////////////////// as a result, I think result3 was correctly

Writing into c-string

倾然丶 夕夏残阳落幕 提交于 2019-12-07 16:31:22
问题 my code segfaults and I don't know why. 1 #include <stdio.h> 2 3 void overwrite(char str[], char x) { 4 int i; 5 for (i = 0; str[i] != '\0'; i++) 6 str[i] = x; 7 } 8 9 int main(void) { 10 char *s = "abcde"; 11 char x = 'X'; 12 overwrite(s, x); 13 printf("%s\n", s); 14 return 0; 15 } The gdb debugger tells me, that problem is on the line 6, where I want to store a char, into c-string (if I use lvalue pointer dereferencing, it's the same problem.) This is what he says: (gdb) run Starting

Is there a safe way to assert if a string view is null terminated?

寵の児 提交于 2019-12-06 07:50:13
I have some part in my code that uses string view extensively. It would be inconceivable to use std::string everywhere, and char const* won't work since there is associative containers, many comparisons and such operations that are hard to do with plain raw strings. However, there is a place that will eventually deal with a C API, and it needs null terminated strings: auto sv = std::string_view{/* ... */}; c_api(sv.data()); Although this works fine in my case, I'd like to be sure everything is okay and assert that the strings are null terminated, as my system constructing the string views and

How to correctly convert unsigned char to CString and once again, reversely convert to the result of CString to unsigned char?

冷暖自知 提交于 2019-12-06 03:39:52
I have problems with Converting unsigned char to CString and reversely, converting the result of converted CString to unsigned char. first, I try to using CString.append();. /////////////////////////// CString result2; CString result3; unsigend char unchar_result[9]; unchar_result = {143, 116, 106, 224, 101, 104, 57, 157}; unchar_result[8] = NULL; for(int i=0; i<8; i++){ result2=(char)temp[i]; result3.append(result2); } ////////////////////////// as a result, I think result3 was correctly substituted. if I saw 'unchar_result' by the ASCII code, then unchar_result = "?tj?eh9?" and result3 was "

Convert char** (c) of unknown length to vector<string> (c++) [duplicate]

半城伤御伤魂 提交于 2019-12-05 11:35:27
This question already has an answer here: converting an array of null terminated const char* strings to a std::vector< std::string > 4 answers copying c array of strings into vector of std::string 4 answers How would one go about converting a C char** to a C++ vector? Is there some built-in functionality one can utilize to do this, or is it better to accomplish it through a series of iterative steps? EDIT: For various reasons, the number of elements in the C array is unknown. It is possible I could pass that as another parameter, but is that absolutely necessary? You can simply use the

Two Dimensional Array of Characters in C

安稳与你 提交于 2019-12-05 07:43:26
问题 Help me to get out of this problem. I'm using GCC on ubuntu12.04. While I write this program to get 5 strings from keyboard n then print these strings on screen. Program is compiled but during execution it takes strings from keyboard but print only last string. The program which I have written is below: void main() { char names[10]; int i,j; for(i=0;i<5;i++) { printf(" Enter a name which you want to register\n"); scanf("%s",names); } for(i=0;i<5;i++) printf(" the names you enter are %s\n",