Non null terminated array of characters in c++

sidharth sharma

Given the following code:

#include<iostream>
int main(){
  char container[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
  for(char* cptr = container; *cptr != 0; cptr++)
    std::cout << *cptr << std::endl;
  return 0;
}

It prints these characters in sequence each time I execute it. I cannot understand why the loop would terminate since I have not explicitly specified any null terminator at the end of the container array. Please help.

Scott Mermelstein

It's just luck, really.

It happens that the area of memory corresponding to container[7] is 0, so you're getting lucky.

Exceeding the bounds of your array is undefined behavior. In your case, it just happens to be the behavior you were hoping for, but you can't rely on that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

x86 assembly compare with null terminated array

From Dev

C - why is only char array null terminated?

From Dev

How are null-terminated strings terminated in C++11?

From Dev

NULL-terminated array of struct types in C

From Dev

Not Null Terminated character array

From Dev

C++ Debug Assertation Failed: "String is not null terminated"

From Dev

Length of null terminated string in C/C++

From Dev

Null terminated character array vs string object

From Dev

C++ ZLib GZipStream Decompression NULL terminated

From Dev

Checking validity of non null-terminated string

From Dev

What is wrong with adding null character to non null-terminated string?

From Dev

Copy null terminated char array to std::string respecting buffer length

From Dev

Where are the null-terminated strings when converting from C to assembly?

From Dev

Comparing non null-terminated char arrays

From Dev

How to output non-terminated char array to stdout

From Dev

How are null-terminated strings terminated in C++11?

From Dev

How do I represent a null terminated string array in Delphi?

From Dev

Reading a null terminated string from a binary file c++

From Dev

null terminated char array (c-string) not printing without for loop

From Dev

c++ null terminated array of objects

From Dev

C++ copying char to a char array (Debug assertion failed) says string is not null terminated

From Dev

Null terminated character array vs string object

From Dev

How to create a multiple null-terminated char array?

From Dev

Is length of std::string consistent despite string having null terminated characters?

From Dev

C: memcpy() with NULL characters

From Dev

Will a uint8_t array be terminated by null similar to a char array?

From Dev

This function does not properly handle non-NULL terminated strings

From Dev

c# Marshal null-terminated string array

From Dev

How to output non null terminated string to iostream, but keep formatting

Related Related

  1. 1

    x86 assembly compare with null terminated array

  2. 2

    C - why is only char array null terminated?

  3. 3

    How are null-terminated strings terminated in C++11?

  4. 4

    NULL-terminated array of struct types in C

  5. 5

    Not Null Terminated character array

  6. 6

    C++ Debug Assertation Failed: "String is not null terminated"

  7. 7

    Length of null terminated string in C/C++

  8. 8

    Null terminated character array vs string object

  9. 9

    C++ ZLib GZipStream Decompression NULL terminated

  10. 10

    Checking validity of non null-terminated string

  11. 11

    What is wrong with adding null character to non null-terminated string?

  12. 12

    Copy null terminated char array to std::string respecting buffer length

  13. 13

    Where are the null-terminated strings when converting from C to assembly?

  14. 14

    Comparing non null-terminated char arrays

  15. 15

    How to output non-terminated char array to stdout

  16. 16

    How are null-terminated strings terminated in C++11?

  17. 17

    How do I represent a null terminated string array in Delphi?

  18. 18

    Reading a null terminated string from a binary file c++

  19. 19

    null terminated char array (c-string) not printing without for loop

  20. 20

    c++ null terminated array of objects

  21. 21

    C++ copying char to a char array (Debug assertion failed) says string is not null terminated

  22. 22

    Null terminated character array vs string object

  23. 23

    How to create a multiple null-terminated char array?

  24. 24

    Is length of std::string consistent despite string having null terminated characters?

  25. 25

    C: memcpy() with NULL characters

  26. 26

    Will a uint8_t array be terminated by null similar to a char array?

  27. 27

    This function does not properly handle non-NULL terminated strings

  28. 28

    c# Marshal null-terminated string array

  29. 29

    How to output non null terminated string to iostream, but keep formatting

HotTag

Archive