By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,248 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,248 IT Pros & Developers. It's quick & easy.

How to concatenate strings that contains has nulls?

P: n/a
jt
I can't seem to find a way to concatenate strings that have nulls within the
string.

I have a string that I need another string that has nulls in it and what to
append
the 2nd string, 3 string and so forth to the 1st string.

Any ideas how to go about this?

Thanks,
jt
Nov 15 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"jt" <jt****@hotmail.com> writes:
I can't seem to find a way to concatenate strings that have nulls
within the string.

I have a string that I need another string that has nulls in it and
what to append the 2nd string, 3 string and so forth to the 1st
string.

Any ideas how to go about this?


It's logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it's not a string.

Since strcat() works with strings, it can't work with the sequences
you're using (as I'm sure you've discovered.

Since you don't have the trailing '\0' to mark the end of your
sequence, you have to have some other way of specifying how long it
is. Once you've done that, you can probably use memcpy() (or
memmove() if there's a possibility of overlap) to copy things around.
The required arguments to memcpy() are determined by the lengths of
the sequences you want to copy. Since you haven't told us how that's
defined, we can't provide more details.

Of course, you still have the same memory allocation issues (making
sure the target is big enough to hold the concatentated result) that
you have with real strings.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #2

P: n/a

"jt" <jt****@hotmail.com> wrote in message
news:h1*******************@tornado.tampabay.rr.com ...
I can't seem to find a way to concatenate strings that have nulls within
the string.
Of course not. By definition, a string cannot contain
embedded null characters. The null character ('\0')
is used to denote the end of a string.

I have a string that I need another string that has nulls in it and what
to append
the 2nd string, 3 string and so forth to the 1st string.

Any ideas how to go about this?


Stop thinking of them as strings. They're simply arrays
of characters. Then look up 'memcpy()' and 'memmove()'.
(Observe the notations in the documentation about overlapping
areas of memory).

-Mike
Nov 15 '05 #3

P: n/a
>I can't seem to find a way to concatenate strings that have nulls within the
string.
If it has nulls internal to it, it's *NOT* a string.
How do you find the end or length of such a non-string?

I have a string
No, you don't. It's NOT a string!
that I need another string
Another *NON*-string!
that has nulls in it and what to
append
the 2nd string, 3 string and so forth to the 1st string.
They AREN'T STRINGS!!
Any ideas how to go about this?


Assuming:
You have as many ByteSequences ByteSequence1, ByteSequence2, ...
as you need, of length ByteSequence1Length, ByteSequence2Length, ...
and that there is sufficient storage at the end of ByteSequence1 to
hold the rest of them:

memcpy(ByteSequence1+ByteSequence1Length, ByteSequence2, ByteSequence2Length);
memcpy(ByteSequence1+ByteSequence1Length+ByteSeque nce2Length,
ByteSequence3, ByteSequence3Length);
memcpy(ByteSequence1+ByteSequence1Length+ByteSeque nce2Length+ByteSequence3Length,
ByteSequence4, ByteSequence4Length);
...

NewByteSequence1Length = ByteSequence1Length + ByteSequence2Length + ... ;
Gordon L. Burditt
Nov 15 '05 #4

P: n/a
Keith Thompson <ks***@mib.org> wrote:
It's logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it's not a string.


But it would be acceptable to refer to the array OP mentioned as a
series of contiguous strings, would it not? The sequence as a whole
is not a string, but the subsequences are, yes?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #5

P: n/a
>> It's logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it's not a string.


But it would be acceptable to refer to the array OP mentioned as a
series of contiguous strings, would it not? The sequence as a whole
is not a string, but the subsequences are, yes?


But you still have no way to figure out when you've reached the
LAST one. And you don't know whether the last \0 is part of
the non-string.

Gordon L. Burditt
Nov 15 '05 #6

P: n/a

jt wrote:
I can't seem to find a way to concatenate strings that have nulls within
the string.
This is impossible. By definition, a null marks the end of a string. You
can't have nulls within a string.

Perhaps you are thinking not of a string, but an array of characters of
known length.

I have a string that I need another string that has nulls in it and what
to append
the 2nd string, 3 string and so forth to the 1st string.
To concatenate two arrays, use memcpy()


Any ideas how to go about this?

Thanks,
jt


--
Remove '.nospam' from e-mail address to reply by e-mail
Nov 15 '05 #7

P: n/a
jt wrote:
I can't seem to find a way to concatenate strings that have nulls within the
string.


Your problem is meaningless. A null terminates a string. There is no
such thing as nulls within a string.
Nov 15 '05 #8

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Keith Thompson <ks***@mib.org> wrote:
It's logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it's not a string.


But it would be acceptable to refer to the array OP mentioned as a
series of contiguous strings, would it not? The sequence as a whole
is not a string, but the subsequences are, yes?


Only if the last character of the array happens to be a '\0';
otherwise it's a series of contiguous strings followed by an
unterminated character array.

In any case, that's probably not a useful way to look at it. They
seem to be just character arrays with '\0' being an ordinary value
with no special significance.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9

P: n/a
jt

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:9f****************@newsread3.news.atl.earthli nk.net...
jt wrote:
I can't seem to find a way to concatenate strings that have nulls within
the string.


Your problem is meaningless. A null terminates a string. There is no
such thing as nulls within a string.


True, a null terminates a string. But, what if that string happen to be a
binary file?
Nov 15 '05 #10

P: n/a
In article <o4********************@tornado.tampabay.rr.com> ,
jt <jt****@hotmail.com> wrote:
True, a null terminates a string. But, what if that string happen to be a
binary file?


Then it is still terminated by the first NUL character.

If you want something that is able to hold NUL characters within it,
then you are NOT using a "string". A string is DEFINED to end
at the first NUL.

If you want to work with groupings of arbitrary binary characters
that might include NUL, then you probably want to work with an array
of char (better yet, unsigned char).
C90 does not offer any mechanism to dynamically determine "the size"
[allocated? initialized? Highest entry written to? Highest entry
read from?] of an array of char. The closest it comes to that is
that if you have declared a character array (not pointer to char!)
then within the same scope you can use sizeof to get to the size:
char foo[123]; /* sizeof(foo) would be 123 * sizeof(char) */

You can't do this across scope boundaries. For example,

int baz(char myarg[]) {
return sizeof myarg;
}

void koz(void) {
char foo[123];
printf( "size of foo was: %d\n", baz(foo) );
}

This will NOT print out 123: C89 does NOT "somehow" transmit the
"real" size when it passes an object in as an argument.
(Instead, you will find that myarg is treated as a pointer to
a character, so the size printed will be the size of a char pointer.)
C99 offers a new mechanism, the variable sized array; if that is
used (and everything is passed appropriately) then you can determine
an object size at a lower level routine.
--
Watch for our new, improved .signatures -- Wittier! Profounder! and
with less than 2 grams of Trite!
Nov 15 '05 #11

P: n/a
On Sat, 24 Sep 2005 02:44:36 GMT, in comp.lang.c , "jt"
<jt****@hotmail.com> wrote:

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:9f****************@newsread3.news.atl.earthl ink.net...
jt wrote:
I can't seem to find a way to concatenate strings that have nulls within
the string.
Your problem is meaningless. A null terminates a string. There is no
such thing as nulls within a string.


True, a null terminates a string. But, what if that string happen to be a
binary file?


All computer data is binary.

And anyway it doesn't matter - the definition of a string is a
sequence of characters terminated by a null. The first null is thus
the end of the string.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #12

P: n/a
Mark McIntyre wrote:

On Sat, 24 Sep 2005 02:44:36 GMT, in comp.lang.c , "jt"
<jt****@hotmail.com> wrote:

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:9f****************@newsread3.news.atl.earthl ink.net...
jt wrote:
I can't seem to find a way to concatenate
strings that have nulls within
the string.

Your problem is meaningless.
A null terminates a string. There is no
such thing as nulls within a string.


True, a null terminates a string.
But, what if that string happen to be a
binary file?


All computer data is binary.


"Binary files" is standard terminology.

The standard describes two kinds of streams:
binary streams and text streams.
Those kinds of streams would go accordingly
to either binary files or text files.

N869
7.19.3 Files
[#2] Binary files are not truncated, except as defined in
7.19.5.3. Whether a write on a text stream causes the
associated file to be truncated beyond that point is
implementation-defined.

--
pete
Nov 15 '05 #13

P: n/a

jt wrote:
I can't seem to find a way to concatenate strings that have nulls within the
string.

I have a string that I need another string that has nulls in it and what to
append
the 2nd string, 3 string and so forth to the 1st string.

Any ideas how to go about this?

Thanks,
jt


Okay, so you want to concatenate strings, but you want to preserve null
characters? You might want to look at the string-handling code of
FreeDOS edlin (available on ibiblio or alt.sources). Also, these
wouldn't be C strings, but BLOBs (Binary Large OBjects) as they contain
the null character, so you'd need to store lengths as well.

Gregory Pietsch

Nov 15 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.