Re: [Dev-C++] strcpy x pointer array
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-03-07 00:46:53
|
How about vect[3] =3D new char[strlen("text")+1];
/Per W
On Fri, 7 Mar 2003, Zodiaq wrote:
> Hello Andr=E9 and all Dev-C++ users,
>
> Friday, March 7, 2003, 12:27:42 AM, Andr=E9 wrote as follows:
>
> AMB> 1. Please consider the following piece of code
>
> AMB> #include <stdio.h>
> AMB> #include <string.h>
> AMB> ...
> AMB> int main()
> AMB> {
> AMB> char *vect[100];
>
> AMB> ...
> AMB> /* line x */ vect[3]=3D"text";
> AMB> ...
> AMB> /* line y */ strcpy(vect[3], "text");
> AMB> ...
> AMB> }
> AMB> ...
>
> AMB> 2. Both commands in lines x and y don=B4t gererate any compilation e=
rrors;
>
> AMB> 3. The command in line x works fine;
>
> You are asigning a new value for pointer at vect[3] which is ok...
>
> AMB> 4. Why the command in line y generates an execution error interrupti=
ng the
> AMB> execution of
> AMB> the program?
>
> You are copying string "text" to const value "text" - pointer from
> line x points at this...
>
> Before line y, first get some space to hold new data:
> vect[3]=3D new char[ strlen( "text" )];
> now strcpy should work fine
>
>
> --
> regards, Zodiaq
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The debugg=
er
> for complex code. Debugging C/C++ programs can leave you feeling lost and
> disoriented. TotalView can help you find your way. Available on major UNI=
X
> and Linux platforms. Try it free. www.etnus.com
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|