Linked Questions
33 questions linked to/from What is a null-terminated string?
0 votes
3 answers
5k views
What is the purpose of buffer = "\0" [duplicate]
I remember reading the explanation on the use of this line of code, but I've read so many books on sockets for the past week that I can't find it anymore. I do remember in the book, they wrote their ...
0 votes
3 answers
481 views
Is std::string terminated with '/0'? [duplicate]
char *pt = "hello"; std::string str = "hello"; Does str also end with '/0' (is null terminated)?
0 votes
2 answers
779 views
declare char variable in c++, why need to add 1 for declare the array size [duplicate]
There is a saying when we declare char variable. We should declare like this -> char ArrayName[Maximum_C-String_Size+1]; For example: char arr[4+1] = {'a', 'b', 'c', 'd'} but arr[4] = {'a', 'b', '...
0 votes
5 answers
493 views
Utility of '\0' in C string [duplicate]
#include <stdio.h> #include <string.h> int main() { char ch[20] = {'h','i'}; int k=strlen(ch); printf("%d",k); return 0; } The output is 2. As far as I ...
0 votes
2 answers
115 views
Why is this code outputting random garbage values at the end? [duplicate]
I was solving a problem on codeforces, but I keep running into unwanted garbage values as output. What can I do? #include <iostream> #include <string> #include <conio.h> #include <...
0 votes
0 answers
136 views
sizeof unsigned char, convert std::string to unsigned char [duplicate]
Can you say me why different size in this example: // 1 std::string str = "Sun Mar 29 16:05:28 2020"; const unsigned char *t{}; t = (unsigned char *) str.c_str(); std::cout << "t: " << t ...
0 votes
0 answers
37 views
initializing first character with zero and not other? [duplicate]
I found below shown piece of code on libcurl website, but I don't get the reason of setting first char to 0 errbuf[0] = 0; ? What about the others? curl = curl_easy_init(); if(curl) { CURLcode res; ...
24 votes
9 answers
12k views
Why do strings in C need to be null terminated?
Just wondering why this is the case. I'm eager to know more about low level languages, and I'm only into the basics of C and this is already confusing me. Do languages like PHP automatically null ...
2 votes
5 answers
542 views
How come std::cout works fine for [] = "12345" but not for [] = {'1','2','3','4','5'}?
I've noticed a weird discrepancy in C++. Say I have this code: const char myChars[] = "12345"; std::cout << myChars; The output is: 12345 However, if I then change it to: const char myChars[]...
8 votes
8 answers
1k views
Understanding how to create atoi; How are characters compared?
I am trying to improve my understanding of C++, pointer arithmetic especially. I use atoi pretty often, but I have rarely given thought as to how it works. Looking up how it is done, I understand it ...
1 vote
3 answers
2k views
Why use a Pointer and its dereference in if statement in c++
I was looking for some c++ code, and was confused by if(ptr && *ptr), what does it do in this case? // Process the data here. We just break the data into separate pieces and // display it ...
1 vote
1 answer
3k views
Comparing uint8_t data with string
This may sounds little odd or question may be a trivial one, but for most of my life I was programming in PHP (yeah, I know how it sounds). So when I switched to C++ there are things quite unfamilliar ...
0 votes
2 answers
3k views
Receiving garbage values while printing character array
I'm writing a code to encrypt the entered text in Caesar Cipher but I'm getting a problem. While running my code my loop is not being terminated at null character. Code is as follows: #include <...
2 votes
5 answers
633 views
Extra characters on cstring when cout
I have a char[4] dataLabel that when I say wav.read(dataLabel, sizeof(dataLabel));//Read data label cout << "Data label:" <<dataLabel << "\n"; I get the output Data label:data� ...
0 votes
1 answer
1k views
Garbage value getting printed when using copy function for a string in c++
I was simply using copy function in c++ for copying a string into an array. But the result shown for the following code contain garbage values: #include <iostream> #include <string> using ...