How to concatenate strings that contains has nulls?

J

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
 
K

Keith Thompson

jt said:
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.
 
M

Mike Wahler

jt said:
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
 
G

Gordon Burditt

I can't seem to find a way to concatenate strings that have nulls within the

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+ByteSequence2Length,
ByteSequence3, ByteSequence3Length);
memcpy(ByteSequence1+ByteSequence1Length+ByteSequence2Length+ByteSequence3Length,
ByteSequence4, ByteSequence4Length);
...

NewByteSequence1Length = ByteSequence1Length + ByteSequence2Length + ... ;


Gordon L. Burditt
 
C

Christopher Benson-Manica

Keith Thompson said:
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?
 
G

Gordon Burditt

It's logically impossible. A "string" is, by definition, "a
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
 
J

James McIninch

jt said:
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()
 
M

Martin Ambuhl

jt said:
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.
 
K

Keith Thompson

Christopher Benson-Manica said:
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.
 
J

jt

Martin Ambuhl said:
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?
 
W

Walter Roberson

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.
 
M

Mark McIntyre

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.
 
P

pete

Mark said:
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.
 
G

Gregory Pietsch

jt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top