#Skip null terminator in c

26 messages · Page 1 of 1 (latest)

primal horizonBOT
#

@hot moth

It looks like you may have code formatting errors in your message

Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks

Markup

```c
int main() {}
```

Result
int main() {}
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

opal crow
#

sprintf()

opal crow
#

hence the leading s on sprintf() -- it stands for "string". or, in full form, sprintf() == "string print format ( )"

prisma spire
#

this is UB. sizeof is wrong here
if you to skip null terminators you'd need some mechanism to determine the length of the string first

opal crow
prisma spire
#

;compile -Wall -Wextra

#include <assert.h>
#include <string.h>
int main(void) {
  char str[42] = "hello, world";
  assert(sizeof str != strlen(str)); 
}```
high joltBOT
#
Compilation successful

No output.

prisma spire
prisma spire
#

up to you. you'd basically need to implement your own string type

#

i don't think you can do it using raw using c-strings

#

So i need to create a string object that doesnt stop with a \0
not really. you make have it stop with a null terminator.

but how will i use all the functions that need strings
well... you can't really use use as is can you? your 'full strings' will be truncated.
implement your own

#

if you're only dealing with small strings - the easiest way i can think of is using pascal strings, reserving the first (or the couple first) char(s) for the length

#

10MiB string? really? you might want to break it up.
if thats the case (for the third time) implement your own string object

#

i don't follow

late kite
#

fwrite(large_string, 1, <size>, stdout);?

#

i dont follow the converstation but im pretty sure you want this

prisma spire
#

is the file consists of ascii text alone? whats wrong with reading it one line at a time then?

prisma spire
#

sorry? what does that means exactly?
why don't you actually state the problem you're trying to solve instead of us trying to query you one thing at a time?

#

sorry, that still makes no sense. what does 'hex representation of file' means? and what that 'saved in string.h' means?

#

you have yet to answer my first question...

as a side note: don't name your header string.h

#

that wasn't my question...

#

regardless - as mentioned above - you'd need to determine the length of the string.
since you (now) mentioned said string it declared in a header, i.e. it has a static duration, sizeof large_string is well defined (since static duration objects are default initialized). in that particular case - you just need to determine when the string actually end, and thats depend on the data you're operating on

assuming 2 consecutive 0s signify the end of data - one can simply do:

for(size_t i = 0; i < sizeof str - 1; i++) {
  if(str[i] == str[i + 1] == 0) break;
  if(str[i] == 0) { printf("\\0"); }
  printf("%c", str[i]);
}```
or something along these lines (this particular case doesn't handle the edge case where `str[sizeof str - 1]` is a valid char. you'd need to solve it on your own)
primal horizonBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity