str* library functions help 
Author Message
 str* library functions help


Quote:
>Hello all,
>I'm having problems with some str* library functions (e.g. strcat, strcpy).
>Can someone help me out with these few questions please?
>1. Does the destination pointer need to be malloc'd with strcpy?
>   E.g. char *s="Hello, world!", *d; strcpy(d, s);
>   Yes?

It has to be a valid pointer, initialized to point to someplace large
enough for the result, as stated in the FAQ. However, the place need
not be allocated via malloc(),

   char *s = "Hello, world", *d, buf[13];

   d = buf;
   strcpy(d, s);

is OK.

Quote:
>2. How about with strdup?

There is no strdup() function in the standard C library.

Quote:
>3. The help file of Borland C++ 4.5 says strcat appends the second string
>   to the end of the first one. How? Suppose:
>char *str1="Hello, ", *str2="world!";
>   how do I append str2 to the end of str1 to get "Hello, world!"?

NOT, since str1 is a string literal, and to concatenate you have
to modify at least one character of that string literal. However,

  char str[14] = "Hello, ", str2 = "world!";

  strcat(str, str2);

is OK.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Sun, 27 Jun 1999 03:00:00 GMT  
 str* library functions help

Hello all,

I'm having problems with some str* library functions (e.g. strcat, strcpy).
Can someone help me out with these few questions please?

1. Does the destination pointer need to be malloc'd with strcpy?
   E.g. char *s="Hello, world!", *d; strcpy(d, s);
   Yes?

2. How about with strdup?

3. The help file of Borland C++ 4.5 says strcat appends the second string
   to the end of the first one. How? Suppose:

char *str1="Hello, ", *str2="world!";

   how do I append str2 to the end of str1 to get "Hello, world!"?

Thank you very much. Any help is very much appreciated.

--
Kevin Yeung
email: keviny at hk dot super dot net

      *** I am _not_ interested in making money on the Internet ***
                *** Do not put me in any mailing list ***



Sun, 27 Jun 1999 03:00:00 GMT  
 str* library functions help



Quote:
> Hello all,

> I'm having problems with some str* library functions (e.g. strcat,
strcpy).
> Can someone help me out with these few questions please?

> 1. Does the destination pointer need to be malloc'd with strcpy?
>    E.g. char *s="Hello, world!", *d; strcpy(d, s);
>    Yes?

You have to have storage for the destination "d".  It does not have to be
created using malloc().  It could be a local, static or global array.  But
"d" 'darned well better' point to a real storage location before you move
data into it.

Quote:
> 2. How about with strdup?

The function strdup() is a non-standard function that copies a string.  You
need to remember to free the strings created with strdup() or you will get
a {*filter*} memory leak.  Programs written with strdup() will be less portable
than those that use arrays or malloc().  If it meets your requirements,
then use it if you like.

Quote:
> 3. The help file of Borland C++ 4.5 says strcat appends the second string
>    to the end of the first one. How? Suppose:

> char *str1="Hello, ", *str2="world!";

>    how do I append str2 to the end of str1 to get "Hello, world!"?

/* you don't. do something like this instead: */
#include <string.h>
#include <stdio.h>
int main( void )
{
    char  *str1 = "Hello, ";
    char  *str2 = "world!";
    char  data_area[255];
    strcpy(data_area, str1);
    strcat(data_area, str2);
    puts(data_area);
    /* There are many other ways to do it. */
    /* HTH ;-) */
    return 0;
Quote:
}



Sun, 27 Jun 1999 03:00:00 GMT  
 str* library functions help

Quote:

> Hello all,

> I'm having problems with some str* library functions (e.g. strcat, strcpy).
> Can someone help me out with these few questions please?

> 1. Does the destination pointer need to be malloc'd with strcpy?
>    E.g. char *s="Hello, world!", *d; strcpy(d, s);
>    Yes?

> 2. How about with strdup?

> 3. The help file of Borland C++ 4.5 says strcat appends the second string
>    to the end of the first one. How? Suppose:

> char *str1="Hello, ", *str2="world!";

>    how do I append str2 to the end of str1 to get "Hello, world!"?

> Thank you very much. Any help is very much appreciated.

> --
> Kevin Yeung
> email: keviny at hk dot super dot net

>       *** I am _not_ interested in making money on the Internet ***
>                 *** Do not put me in any mailing list ***

1. if you are using a pointer (as you were in the example) then yes,
however if you are using an array then no.

char array[30];
strcpy(array, "hello world!");

remember the str* functions accept pointers because that gives the
programer the most options, it does not force you to use pointers

2.  strdup (according to the documentation that came with my compiler)
copies the '\0' terminated string to a memory location created by strdup
(using malloc) and returns a pointer to the newly allocated memory.  The
user is responsible for cleaning up memory later with a call to free.
btw if there is not enough contiguous memory to hold the '\0' terminated
string then strdup returns NULL

3. well the docs don't say anything on this, however I think the code
might look something like this

char *strcat(char *dest, const char *src)
   {
   char *dest_current = dest;
   char *src_current  = src;

   while (*dest_current != '\0')
      dest_current++;
   while (*src_current != '\0')
      {
      *dest_current = *src_current;
      dest_current++;
      src_current++;
      }
    *dest_current = '\0';
    return dest;
    }

at least that is how I implemented it when I had to write all of the
str* functions for my c class.

note any errors in the code are mine, and I take full responsibility for
them, but I did type this in off the top of my head so please flame me
with bics only



Mon, 28 Jun 1999 03:00:00 GMT  
 str* library functions help

In all three cases you have to guarantee that there is enough memory where you
copy (or dup) your string. No str* routine will do this for you.

Robert B. Rossmann



Mon, 28 Jun 1999 03:00:00 GMT  
 str* library functions help

<snip>

Quote:
> char *strcat(char *dest, const char *src)
<snip>
>    char *src_current  = src;

If you are providing an example, it is best not to obfuscate matters
by violating the rules of C :-)

The above line should be

const char *src_current = src;

(Of course, I don't see why one needed to copy away src, but that is
programming style.)

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Mon, 28 Jun 1999 03:00:00 GMT  
 str* library functions help

Quote:

> 1. Does the destination pointer need to be malloc'd with strcpy?
>    E.g. char *s="Hello, world!", *d; strcpy(d, s);
>    Yes?

In your example above, yes, d must be malloc()'ed before you can
store anything in it. d could also be an array of characters.

Quote:
> 2. How about with strdup?

No, strdup() malloc()'s for you.

Quote:
> 3. The help file of Borland C++ 4.5 says strcat appends the second
>    string to the end of the first one. How? Suppose:
> char *str1="Hello, ", *str2="world!";
>    how do I append str2 to the end of str1 to get "Hello, world!"?

Like strcpy(), strcat() assumes that the destination points to
adequate storage for the concatenated result, so str1 in your
example will not do.

Maybe,
R.



Mon, 28 Jun 1999 03:00:00 GMT  
 str* library functions help

Quote:

>In all three cases you have to guarantee that there is enough memory where you
>copy (or dup) your string. No str* routine will do this for you.

While strdup() is not a function from the standard C library, the most
common implementation _does_ allocate memory for the result. This need
not be an advantage, and, as stated in an earlier posting, the function
is non-standard, but an implementation that provides it but makes it
do something unexpected, like, e.g., not allocating the memory for the
result, should better have a good reason to do so.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Mon, 28 Jun 1999 03:00:00 GMT  
 str* library functions help

Quote:

> 1. Does the destination pointer need to be malloc'd with strcpy?
>    E.g. char *s="Hello, world!", *d; strcpy(d, s);
>    Yes?

The destination must have space to store the copied string - whether you
get that space with malloc(), or by declaring the destination as a char
array of suitable size is up to you.

Note that arrays act like pointers to their first element when used in
"value contexts", as when passed as fucntion parameters, so when a
string function is shown as taking char pointers as parameters, it will
be perfectly happy with char arrays as well.

Quote:

> 2. How about with strdup?

Borland's manual page fro strdup() says that the function obtains the
space itself, with a call to malloc() - you don't have to worry about
getting the space (but you might want to free() it later)

Quote:

> 3. The help file of Borland C++ 4.5 says strcat appends the second string
>    to the end of the first one. How? Suppose:

> char *str1="Hello, ", *str2="world!";

>    how do I append str2 to the end of str1 to get "Hello, world!"?

Well, the above declarations won't work - you cannot portably change a
string literal such as you have declared above.  On some systems, those
strings will be located in non-writable memory.

The following will work:
char str1[40] = "Hello ";
char *str2 = "World!";
strcat(str1, str2);

--
Peter Bennett, VE7CEI    Vancouver, B.C., Canada

GPS and NMEA info and programs:
ftp://sundae.triumf.ca/pub/peter/index.html
or:
ftp://ftp-i2.informatik.rwth-aachen.de/pub/arndt/GPS/peter/index.html
or: http://vancouver-webpages.com/peter/index.html



Mon, 28 Jun 1999 03:00:00 GMT  
 str* library functions help

Quote:

> Hello all,

> I'm having problems with some str* library functions (e.g. strcat, strcpy).
> Can someone help me out with these few questions please?

> 1. Does the destination pointer need to be malloc'd with strcpy?
>    E.g. char *s="Hello, world!", *d; strcpy(d, s);
>    Yes?

If the string is a static array (allocated during compile time), then
you do not need to allocate memory to it. Otherwise, you do.
char str[80] ; /* you do not need to use malloc */
char *str ;    /* you DO need to use malloc */

Quote:
> 2. How about with strdup?

> 3. The help file of Borland C++ 4.5 says strcat appends the second string
>    to the end of the first one. How? Suppose:

> char *str1="Hello, ", *str2="world!";

>    how do I append str2 to the end of str1 to get "Hello, world!"?

/* this is one way to do it */
  char *str1, *str2 ;

  str1 = (char *)malloc(30) ; /* 30 is an arbitrary size*/
  strcpy(str1, "Hello, ") ;

  str = (char *)malloc(30) ; /* 30 is an arbitrary size*/
  strcpy(str2, "world!") ;

  strcat(str1, str2) ;

Quote:
> Thank you very much. Any help is very much appreciated.
> -- Kevin Yeung
> email: keviny at hk dot super dot net

You're welcome. I hope I was able to help.
-Ryan Maroney


Mon, 28 Jun 1999 03:00:00 GMT  
 str* library functions help

Quote:


> <snip>
> > char *strcat(char *dest, const char *src)
> <snip>
> >    char *src_current  = src;

> If you are providing an example, it is best not to obfuscate matters
> by violating the rules of C :-)

> The above line should be

> const char *src_current = src;

> (Of course, I don't see why one needed to copy away src, but that is
> programming style.)

> Cheers
> Tanmoy
> --

> Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
> Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
> <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
> internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
> fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]

if current_src had been declared const then wouldn't the ++  have been
illegale?  As it is I get away with only a suspicious pointer
conversion.


Wed, 30 Jun 1999 03:00:00 GMT  
 str* library functions help

<snip>

Quote:
> > const char *src_current = src;
<snip>
> if current_src had been declared const then wouldn't the ++  have been
> illegale?  As it is I get away with only a suspicious pointer
> conversion.

Your compiler was required to *diagnose* your error, it did a
disservice to you by diagnosing it as a `suspicious pointer
conversion'. Without a cast the conversion is pure illegal, a compiler
could have rejected it, and for people like you who disregard
warnings, it should have done so. :-)

In any case, const char *src_current does not declare current_src to
be anything :-). It also does not declare src_current to be const, it
simply states that what src_current points to is not going to be
modified using *src_current or similar expressions. So, no, your ++
would have been perfectly fine.

And, incidentally, why didn't you check your assertion before posting?

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Wed, 30 Jun 1999 03:00:00 GMT  
 str* library functions help

...

Quote:
>if current_src had been declared const then wouldn't the ++  have been
>illegale?  As it is I get away with only a suspicious pointer
>conversion.

No, const char * is "pointer to const char". The pointer itself is
modifiable, you just can write to a char _through_ it. If you had
char *const current_src; then you wouldn't be able to modify current_src.

--
-----------------------------------------


-----------------------------------------



Thu, 01 Jul 1999 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Mem functions versus str functions

2. ANN: C++ Str Library updated to 2.1.2

3. char **str vs. char *str[]

4. _T(str) vs (CString)str?

5. char *str vs. char str[]

6. Dialogs: m_myCtrl.GetWindowText(str) or GetDlgItemText(IDC_MY_CTRL, str)?

7. ANN: Str Library 2.1.4

8. ANN: C++ Str Library updated to 2.1.2

9. str function prob

10. mem* versus str* functions

11. str* functions and NULL pointers

12. Calling a VB function from a C dll with a str param

 

 
Powered by phpBB® Forum Software