Questions tagged [null-terminated]

A string that ends with (and does not include) a "null" (ASCII 0) byte, as used by the C language

Filter by
Sorted by
Tagged with
3 votes
1 answer
52 views

sizeof('\0') null terminator as literal is four bytes but how come in string of characters it takes only one byte?

In c '\0' null-terminator as literal takes 4-bytes (as it's just zero internally) but how come it takes only 1-byte when used in an array of characters or string of characters? Is this compiler magic? ...
2 votes
1 answer
79 views

How does a String terminate in C?

I know the string in c will be terminated by a character \0. However, if I do char a[5]="abcd\n" , where would \0 be? Or do I need to reserve at least one position for \0, whenever I try to ...
0 votes
1 answer
51 views

parse a char vector to different strings

I have a character vector like std::vector<char> input({'{', 'a', 'b','}',\0', '{','c','d','}',\0','\0','\0'}); I want to parse this to have strings string1="ab" string2="cd"...
0 votes
2 answers
98 views

include_str for null terminated string

I need to read a file into a null terminated string at compile time. Working in Rust OpenGL. I have a shader source code stored in a separate file. The function that will eventually read the source ...
  • 632
3 votes
2 answers
65 views

How does realloc treat null bytes in strings?

Relatively new C programmer here. I am reviewing the following code for a tutorial for a side project I am working on to practice C. The point of the abuf struct is to create a string that can be ...
0 votes
2 answers
55 views

In this solution, if *inputString is a memory address (and the input is a char type) then does *inputString qualify as an array?

I was solving a challenge on CodeSignal in C. Even though the correct libraries where included, I couldn't use the strrev function in the IDE, so I looked up a similar solution and modified it to work....
1 vote
1 answer
207 views

Append a null terminator to a C++ string

In order to implement a client to some protocol that expects null-byte padding to a specific string, I implemented a functions that pads a string with a specific amount of null bytes: string padToFill(...
  • 33
-1 votes
1 answer
36 views

fprintf on (unterminated?) C-string doesn't spew garbage or segfault -- undefined behavior?

I recently learned (initially from here) how to use mmap to quickly read a file in C, as in this example code: // main.c #include <errno.h> #include <fcntl.h> #include <stdio.h> #...
  • 5,085
1 vote
1 answer
203 views

Character array without a null terminator in C

Sometimes I am using sequences of characters (strings) except the null terminator is not needed or wanted, for example if I am using memcpy() and the length is already known. A such, I prefer to omit ...
  • 2,226
0 votes
0 answers
155 views

How to save a text file as an array of strings in MIPS

I am creating a program to read a list of strings from a text file, and store them in an array (in memory, but formatted like an array of strings). I am on Windows running MIPS on MARS. The text file ...
  • 141
5 votes
3 answers
995 views

How do I print a vector of chars using fmt?

I have a const std::vector<char> - not null-terminated. I want to print it using the fmt library, without making a copy of the vector. I would have hoped that specifying the precision would ...
  • 113k
2 votes
1 answer
810 views

What are null-terminated strings?

How are they different from non-null-terminated strings? What is this null that terminates the string? Is it different from NULL? Should I null-terminate my strings myself, or the compiler will do it ...
  • 110k
0 votes
1 answer
192 views

Null character is not acting as end of string in assembly (NASM)

In c language we can use '\0' null character as end of string. #include <stdio.h> int main() { char msg[] = "hello\0world"; printf("msg = %s", msg); } This code will ...
-1 votes
1 answer
91 views

Non null-terminated string reading standard input (Assembly Language)

I'm working on making a standard library from scratch in assembly language using NASM. I'm not doing this to use it in huge project, but just to train myself with assembly language making a project ...
  • 198
0 votes
0 answers
75 views

NASM: Store a null-terminated string in reserved memory [duplicate]

I need to store a null-terminated string in reserved memory. Here is my code so far: helloworld: db "Hello, World!",0 _start: lea ebx, [helloworld] call loop loop: mov al, [ebx]...
0 votes
2 answers
101 views

In C, how do I extract the different parts of a string that are all separated by the \0 null terminator?

So in the following code, I want to store each section in the corresponding variable. Is there an easier way to do this rather than looping through the entire string char by char and checking if it's ...
0 votes
0 answers
169 views

nasm zero byte omitted at the end of the string

I am studying Assembly language using this nasm tutorial. Here is the code that prints a string: SECTION .data msg db 'Hello!', 0Ah SECTION .text global _start _start: mov ebx, ...
  • 22k
0 votes
0 answers
50 views

in the C ++ stl, does the string container actually contain a string with a closing 0? [duplicate]

string s; In C ++ string is a vector == pointer to start + length. But will there be a closing 0, as implicitly implied by Prat ? class String{ private: char * str; // pointer to string int len; // ...
-4 votes
1 answer
165 views

When does a char* ends with null in c?

I have confusion on char* null-termination so i have decided to make a study of cases i can find. Do these string literals end with a null? char str1[512]="This is a random string" char *...
0 votes
2 answers
213 views

Input in Null terminating character array and getting its length

I hava a question.....I have a character array char command[30]. When I use it for input like if I entered !! on console the after input strlength function must give me length of array equals to 2 as ...
0 votes
1 answer
209 views

Checking for string null terminators in C

I know what the null-terminator in C is represented by \0 and has the numerical value of 0. However, when I execute the following code below, the program treats the null terminator as %. I searched ...
0 votes
3 answers
135 views

How to Replace Leading or Trailing Blank Characters with "X"

Looking for a more efficient way to replace leading and trailing empty spaces (' ') and appending an 'X' to the front for each empty space.. It seems to work ok for trailing spaces but I'd like to ...
0 votes
1 answer
95 views

Avoiding the func(char *) api on embedded

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 ...
2 votes
1 answer
163 views

How does a program determine the end of a string?

I can define a new variable like so msg db 'Hello, world!$', or another way msg2 db 'Hello, world!', 0 I know that the end of a string is determined using value 0 in memory. What is symbol $ standing ...
2 votes
2 answers
2k views

if I initialize a char array to zero/{0} do I have to null terminate?

Say for instance I declare a char array with all values set to zero in the following fashion: char array[4] = {0}; If I assign it values, for instance: array[0] = 'A'; array[1] = 'B'; array[2] = 'C'; ...
1 vote
1 answer
357 views

c_str() returns nothing, but string not empty

So, I have a class class MySuperClass { public: std::string buffer; }; And wants to print buffer to std::cout. Here's some code about filling string from a file: MySuperClass msc; std::fstream file(&...
  • 25
0 votes
0 answers
362 views

how to null terminate a string in assembly language?

I am wondering how can I properly null-terminate a string in assembly language: what I did was simply setting movq $0, (position_to_terminate) and I thought this will terminate my string. However, ...
  • 203
0 votes
2 answers
2k views

Does snprintf/sprintf overwrite the terminating-null of the penultimate argument, just like strcat does?

It is clearly written for strcat, e.g. here and here that, in case, char *strcat(char *s1, const char *s2); then, The initial character of s2 overwrites the null character at the end of s1. But ...
  • 3,359
1 vote
2 answers
893 views

Does '\0' Take Up The Size of a Char? - C

So I am creating a char to string converter and I am creating a string using malloc. I know I need to make space for the null terminating character \0. So do I do char *example = malloc(sizeof(char) + ...
user avatar
0 votes
1 answer
341 views

Weird characters at the end of char array C

I've tried to write an implementation of the Weasel Program. I have compiled three versions, all exactly identical, with the names "weasel.exe", "weasel2.exe", and "weasel3....
0 votes
1 answer
135 views

skip strtok's null terminators safely

I want to use strtok and then return the string after the null terminator that strtok has placed. char *foo(char *bar) { strtok(bar, " "); return after_strtok_null(bar); } /* examples: foo(&...
  • 227
0 votes
0 answers
56 views

How does Java know the length of strings? [duplicate]

I was searching if Java strings are null-terminated like in C and came across the first answer for this:Does java define string as null terminated? Basically the link mentions that the length method ...
  • 326
1 vote
2 answers
189 views

Two null termination signs at the end of a string in C

I'm learning C via "The C Programming Language" book. During one of the exercises, where it's needed to concatenate two strings, I found that there are two null terminating signs (\0) at the end of a ...
  • 13
3 votes
3 answers
318 views

Advantages of null termination vs count variable

I need to use array of graph data, i.e. struct with x and y integers. This array will be passed through many functions, and I need to decide the API choice. typedef struct { int x; int y; } ...
  • 63
2 votes
3 answers
285 views

read null terminated string from byte vector in julia

I have a vector of type UInt8 and fixed length 10. I think it contains a null-terminated string but when I do String(v) it shows the string + all of the zeros of the rest of the vector. v = zeros(...
  • 113
2 votes
1 answer
129 views

Why does this string implementation allocate one more character than there are characters in the string?

String::String(const String& old_str) { size = old_str.size; s = new char[size+1]; Why do we use size+1 here to allocate memory, not size?
  • 25
11 votes
3 answers
275 views

What are the specifics of the definition of a string in C?

I am supposed to answer a homework question for one of my classes. Specifically, I am supposed to say if certain arrays in C are considered strings or not. Based on this article (https://www....
  • 135
4 votes
2 answers
3k views

Is it safe to convert function parameter `const char*` to `std::string_view`?

The title almost says it all. void f(const char*) to void f(std::string_view) Is it safe? if not, what are the pitfalls? std::string_view semantics dictate that the array isn't necessarily null ...
  • 10.4k
0 votes
3 answers
75 views

Unexplainable behaviour when printing out strings in C

The following code works as expected and outputs ABC: #include <stdio.h> void printString (char toPrint [100]); int main() { char hello [100]; hello[0] = 'A'; hello[1] = 'B'; hello[2] ...
  • 47
4 votes
3 answers
2k views

A null character '\0' at the end of a string

I have the following code. #include <stdio.h> #include <string.h> #define MAXLINE 1000 int main() { int i; char s[MAXLINE]; for (i = 0; i < 20; ++i) { s[i] = i + ...
  • 91
-1 votes
2 answers
603 views

Char pointer NULL termination and memory allocation [duplicate]

I have the following program : My program compiles fine and gives the output as mentioned below . I have some question on output which is listed at the bottom. *****************************************...
  • 1
0 votes
1 answer
291 views

How to store multiple 2D char arrays using HDF5 in C++?

I want to write multiple 2D char arrays into one HDF5 dataset. In the code below, there are two arrays in a struct and I put(std::copy) "hello"s to one array(name) and "hi"s to the other(nicknams). I ...
  • 1,924
0 votes
1 answer
178 views

How to efficiently read a null terminated, non unicode string from unmanaged memory

I am needing to read a few thousand null terminated C strings from memory. My current solution is as follows byte* stringPointer; // Obviously has an actual value in my codebase var stringLength = ...
0 votes
4 answers
197 views

How to terminate variable length array when all values are valid?

I'm passing an array of single-precision floating point values to a function in C. The function has no knowledge of the size of the array and I'd like to keep it that way, primarily because while the ...
0 votes
0 answers
289 views

Why is Redshift throwing error code 1218 for a string that is NOT null terminated?

I am querying the STL_LOAD table in redshift and I am seeing this error: Invalid null byte - field longer than 1 byte The error code is 1218. According to their documentation: When using NULL AS '\...
0 votes
2 answers
597 views

Why does multiple fgets statements overwrite character arrays?

The following code works, but if I input more than 10 characters (let's say 10 a's), the output turns into something like this: "Dog's name? aaaaaaaaaDog's breed?Dog's name: aaaaaaaaaDog's breed:" ...
7 votes
1 answer
4k views

How to initialize a char array without the null terminator?

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 ...
  • 12.9k
0 votes
1 answer
978 views

How python support null characters in string? [duplicate]

I see python allows null characters in its strings. So it's implementation is not based on null-terminated string? But C only support null-terminated strings. Could anybody help explain how python ...
  • 11.4k
0 votes
2 answers
55 views

Support strings with null charcters in C [closed]

C assumes strings are null terminated. To support strings with null characters in them, is there anything readily available in C?
  • 11.4k
2 votes
4 answers
507 views

Is it Safe to strncpy Into a string That Doesn't Have Room for the Null Terminator?

Consider the following code: const char foo[] = "lorem ipsum"; // foo is an array of 12 characters const auto length = strlen(foo); // length is 11 string bar(length, '\0'); // bar was constructed ...
  • 37.5k

1
2 3 4 5