Non-zero-terimated string

This is a discussion on Non-zero-terimated string within the C++ Programming forums, part of the General Programming Boards category; How do I get a char* that doesn't point to a zero-terminated array of characters? How can I create "fake ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Non-zero-terimated string

    How do I get a char* that doesn't point to a zero-terminated array of characters?
    How can I create "fake C-style strings" on the free store and stack?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps:
    Code:
    char fake[] = {'f', 'a', 'k', 'e'};
    const unsigned size = sizeof(fake);
    char* fake_2 = new char[size];
    std::copy(fake, fake + size, fake_2);
    Last edited by anon; 04-22-2008 at 01:54 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jw232
    How do I get a char* that doesn't point to a zero-terminated array of characters?
    Why would you want to?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    An array is an array. It's just that when you use string literals in double quotes they are automatically terminated with '\0'.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by cpjust View Post
    Why would you want to?
    Why would you want to point at a buffer of data? I can think of about an infinite number of reasons.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    Why would you want to point at a buffer of data? I can think of about an infinite number of reasons.
    Then it's not a valid string anymore, it's just data.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by cpjust View Post
    Then it's not a valid string anymore, it's just data.
    According to what definition? There are plenty of reasons to want to manipulate strings of text which, for whatever reason, contain embedded nulls. You just can't do it with the standard string processing functions.

    I don't believe the standard ever explicitly spells out what a "string" is, although it does describe what a "string literal" is, and it describes the string processing functions in terms of acting on sequences of bytes with a null terminator.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    According to what definition? There are plenty of reasons to want to manipulate strings of text which, for whatever reason, contain embedded nulls. You just can't do it with the standard string processing functions.

    I don't believe the standard ever explicitly spells out what a "string" is, although it does describe what a "string literal" is, and it describes the string processing functions in terms of acting on sequences of bytes with a null terminator.
    I don't really have the biggest imagination, so the only kind of string I can think of with embedded NULLs would be something like Unicode.
    What other kinds of strings are you thinking of?

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by cpjust View Post
    I don't really have the biggest imagination, so the only kind of string I can think of with embedded NULLs would be something like Unicode.
    What other kinds of strings are you thinking of?
    That's one good example. I guess it goes to show how deep the C-style string concept sits in most peoples' minds, that you have a hard time imagining a string with a null in it.

    Anyway, this is a valid string which contains a null: "This string \0 has a null in it." It's not for us to ask why

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by cpjust View Post
    I don't really have the biggest imagination, so the only kind of string I can think of with embedded NULLs would be something like Unicode.
    What other kinds of strings are you thinking of?
    Raw data read from a file/socket/pipe etc. into an unsigned char buffer?

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    File dialog filters usually have a number of embedded \0 to separate the parts.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by medievalelks View Post
    Raw data read from a file/socket/pipe etc. into an unsigned char buffer?
    But that's not a string, is it? Binary data can have NULL... that's a pretty granted.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    But that's not a string, is it? Binary data can have NULL... that's a pretty granted.
    Exactly. He was looking for examples of char buffers that could logically contain '\0' and not represent C-style strings.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Ah, I was looking at C-style strings, not char buffers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Elysia View Post
    But that's not a string, is it? Binary data can have NULL... that's a pretty granted.
    you constantly call nul-character "NULL". It is wrong. NULL is pointer and as so cannot be assigned to char or any other integer type...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21