Re: strcpy HELP!!!
Lee Brinton:
> Scott D. Killen writes:
> > You are copying the string to temp2 which has not been initialized and is
> a
> > NULL pointer. You need to allocate memory of at least the same size as
> > temp1 to temp2 and
> > this will solve your problem.
>
> When using C library string functions always allocate memory of at least the
> length of the string pointed to by temp1 plus 1 byte to hold the string
> termination character '\0'.
>
> As in:
>
> temp2 = (char*)malloc(strlen(temp1) + 1);
No they don't always. From the strcpy man page:
The strcpy() function copies the string pointed to be src
(including the terminating `\0' character) to the array
pointed to by dest. The strings may not overlap, and the
destination string dest must be large enough to receive
the copy.
You have to allocate the space by yourself. However, there is strdup:
NAME
strdup - duplicate a string
SYNOPSIS
#include <string.h>
char *strdup(const char *s);
DESCRIPTION
The strdup() function returns a pointer to a new string
which is a duplicate of the string s. Memory for the new
string is obtained with malloc(3), and can be freed with
free(3).
This function actually does the memory allocation for you. More
information for aspiring C-programmers can be found typing `info libc',
provided you have the glibcdoc package installed (at least, that is
where the info file is kept on a bo system).
Eric Meijer
--
E.L. Meijer (tgakem@chem.tue.nl) | tel. office +31 40 2472189
Eindhoven Univ. of Technology | tel. lab. +31 40 2475032
Lab. for Catalysis and Inorg. Chem. (TAK) | tel. fax +31 40 2455054
--
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Reply to: