transforming from a non-null terminated string into a null terminated string

transforming from a non-null terminated string into a null terminated string

Post by ssyle » Thu, 14 Aug 2008 04:06:41


If I'm being supplied with a char* that is not null-terminated, is it
impossible to transform it into a null terminated char* with only
abstract information about the char* information?
 
 
 

transforming from a non-null terminated string into a null terminated string

Post by santos » Thu, 14 Aug 2008 04:15:24


It depends. One piece of information that you *must* know is *where* to
place the null character, i.e., you must know the length of the char
array that your char* points at. Otherwise you'll either truncate your
array or write beyond it's bounds.

What abstract information have you been given about the char*?

 
 
 

transforming from a non-null terminated string into a null terminated string

Post by Richard He » Thu, 14 Aug 2008 04:19:27

ssylee said:


A char * is a pointer to a single character. Presumably you mean that the
char * points to the first character in an arbitrarily long sequence, a
sequence which ought to have a null terminator but, for one reason or
another, has not. My answer is based on that understanding.


If you know where the null terminator is supposed to go, you can put one
there. s[n] = '\0';

If you don't, you can't, because a non-null-terminated sequence of chars
will not of itself provide you with that information (in the general
case). I am ignoring clever-clever answers such as:

char arr[55] = "The terminator belongs in place of this trailing colon:";
char *p = arr;

for what I hope are obvious reasons.

So no, you can't hope for the language to tell you where your data ends -
it's your job to know that.

--
Richard Heathfield < http://www.yqcomputer.com/ >
Email: -http://www. +rjh@
Google users: < http://www.yqcomputer.com/ ;
"Usenet is a strange place" - dmr 29 July 1999
 
 
 

transforming from a non-null terminated string into a null terminated string

Post by ssyle » Thu, 14 Aug 2008 05:02:51


Thank you for your replies. I found out that there is an information
that I have missed out in terms of the ASCII to UNICODE conversions.
So I guess this problem is solved.
 
 
 

transforming from a non-null terminated string into a null terminated string

Post by Martin Amb » Thu, 14 Aug 2008 05:37:42


It depends on what that "abstract information" is. If it includes the
length of the information you want in the string, or if it indicates
that the 1st (or 2nd or some other specific ordinal) occurrence of a
certain character is the end of the wanted resultant string, obviously
it is trivial. Otherwise, it is near impossible.
 
 
 

transforming from a non-null terminated string into a null terminated string

Post by CBFalcone » Thu, 14 Aug 2008 06:57:55


What specifies the length of the string you have stored in there?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: < http://www.yqcomputer.com/ >
Try the download section.