[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

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

ssylee

8/12/2008 7:07:00 PM

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?
7 Answers

santosh

8/12/2008 7:15:00 PM

0

ssylee wrote:

> 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?

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*?

Richard Heathfield

8/12/2008 7:19:00 PM

0

ssylee said:

> If I'm being supplied with a char* that is not null-terminated,

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.

> is it
> impossible to transform it into a null terminated char* with only
> abstract information about the char* information?

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.cpax....
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/goog...
"Usenet is a strange place" - dmr 29 July 1999

ssylee

8/12/2008 8:03:00 PM

0

On Aug 12, 12:15 pm, santosh <santosh....@gmail.com> wrote:
> ssylee wrote:
> > 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?
>
> 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*?

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.

Martin Ambuhl

8/12/2008 8:38:00 PM

0

ssylee wrote:
> 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?

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.

CBFalconer

8/12/2008 9:58:00 PM

0

ssylee wrote:
>
> 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?

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

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.a...
Try the download section.

gelderen64

6/23/2014 3:10:00 PM

0

The Federal stance concerning secession in my mind has always made the USA look like the roach motel: States may check in but they can never leave.

Bradipus

6/23/2014 6:14:00 PM

0

VGer47 17:09, luned? 23 giugno 2014:

> The Federal stance concerning secession in my mind has always
> made the USA look like the roach motel: States may check in
> but they can never leave.


That looks like the Eurozone...


--
Bradipus