null-terminated

Avoiding the func(char *) api on embedded

心已入冬 提交于 2021-01-29 08:14:24
问题 Note: I heavily changed my question to be more specific, but I will keep the old question at end of the post, in case it is useful to anyone. New Question I am developing an embedded application which uses the following types to represent strings : string literals(null terminated by default) std::array<char,size> (not null terminated) std::string_view I would like to have a function that accepts all of them in a uniform way. The only problem is that if the input is a string literal I will

How to initialize a char array without the null terminator?

好久不见. 提交于 2020-07-20 10:53:14
问题 The char array is a part of network message, which has well defined length, so the null terminator is not needed. struct Cmd { char cmd[4]; int arg; } struct Cmd cmd { "ABCD" , 0 }; // this would be buffer overflow How can I initialize this cmd member char array? without using functions like strncpy ? 回答1: Terminating null character is ignored if the size of the char array is the same as the number of characters in the initializer. So cmd will not have the null terminator. The relevant

How to initialize a char array without the null terminator?

[亡魂溺海] 提交于 2020-07-20 10:52:31
问题 The char array is a part of network message, which has well defined length, so the null terminator is not needed. struct Cmd { char cmd[4]; int arg; } struct Cmd cmd { "ABCD" , 0 }; // this would be buffer overflow How can I initialize this cmd member char array? without using functions like strncpy ? 回答1: Terminating null character is ignored if the size of the char array is the same as the number of characters in the initializer. So cmd will not have the null terminator. The relevant

Why/when to include terminating '\0' character for C Strings?

拈花ヽ惹草 提交于 2020-01-31 20:12:27
问题 I'm very new to C and am a bit confused as to when we need to manually add the terminating '\0' character to strings. Given this function to calculate string length (for clarity's sake): int stringLength(char string[]) { int i = 0; while (string[i] != '\0') { i++; } return i; } which calculates the string's length based on the null terminating character. So, using the following cases, what is the role of the '\0' character, if any? Case 1: char * stack1 = "stack"; printf("WORD %s\n", stack1);

Why/when to include terminating '\0' character for C Strings?

耗尽温柔 提交于 2020-01-31 20:11:59
问题 I'm very new to C and am a bit confused as to when we need to manually add the terminating '\0' character to strings. Given this function to calculate string length (for clarity's sake): int stringLength(char string[]) { int i = 0; while (string[i] != '\0') { i++; } return i; } which calculates the string's length based on the null terminating character. So, using the following cases, what is the role of the '\0' character, if any? Case 1: char * stack1 = "stack"; printf("WORD %s\n", stack1);

C: String Concatentation: Null Terminated Strings

删除回忆录丶 提交于 2020-01-05 06:53:21
问题 The following code only concatenates the first string and ignores the second.. from what I gather, it has something to do with Null terminated strings. As I am new to C, this is a new concept to me. Could someone help make the code below work? That would really help me a lot in understanding this. void concatTest(); int main() { concatTest(); system("PAUSE"); return 0; } void concatTest() { char string1[20], string2[20], string3[40]; char *ptr1, *ptr2, *ptr3; ptr1 = &string1[0]; ptr2 =

Remove trailing NULL terminator

只谈情不闲聊 提交于 2019-12-25 18:58:25
问题 I have a large char array that is filled with 0's. I read an incoming file from a socket and place it's contents in the buffer. I can't write the buffer with all of the '\0's in it, so I allocate a new buffer with the correct size and to write. I used this approach to do that: int i = 0; while(buf[i] != '\0'){ i++; } char new[i]; while(i){ new[i] = buf[i]; i--; } new[0] = buf[0]; While this approach works, it doesn't seem like the smartest or most elegant way. What is the best way to remove

Searching for strings that are NULL terminated within a file where they are not NULL terminated

大兔子大兔子 提交于 2019-12-25 08:58:33
问题 I am writing a program that opens two files for reading: the first file contains 20 names which I store in an array of the form Names[0] = John\0 . The second file is a large text file that contains many occurences of each of the 20 names. I need my program to scan the entirity of the second file and each time it finds one of the names, a variable Count is incremented and so on the completion of the program, the total number of all the names appearing in the text is stored in Count . Here is

Can a std::string contain embedded nulls?

最后都变了- 提交于 2019-12-17 16:30:14
问题 For regular C strings, a null character '\0' signifies the end of data. What about std::string , can I have a string with embedded null characters? 回答1: Yes you can have embedded nulls in your std::string . Example: std::string s; s.push_back('\0'); s.push_back('a'); assert(s.length() == 2); Note: std::string 's c_str() member will always append a null character to the returned char buffer; However, std::string 's data() member may or may not append a null character to the returned char

Copying non null-terminated unsigned char array to std::string

早过忘川 提交于 2019-12-17 10:38:20
问题 If the array was null-terminated this would be pretty straight forward: unsigned char u_array[4] = { 'a', 's', 'd', '\0' }; std::string str = reinterpret_cast<char*>(u_array); std::cout << "-> " << str << std::endl; However, I wonder what is the most appropriate way to copy a non null-terminated unsigned char array, like the following: unsigned char u_array[4] = { 'a', 's', 'd', 'f' }; into a std::string . Is there any way to do it without iterating over the unsigned char array ? Thank you