Re: [Dev-C++] strcpy x pointer array
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Zodiaq <zo...@ac...> - 2003-03-06 23:54:13
|
Hello André and all Dev-C++ users,
Friday, March 7, 2003, 12:27:42 AM, André 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]="text";
AMB> ...
AMB> /* line y */ strcpy(vect[3], "text");
AMB> ...
AMB> }
AMB> ...
AMB> 2. Both commands in lines x and y don´t gererate any compilation errors;
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 interrupting 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]= new char[ strlen( "text" )];
now strcpy should work fine
--
regards, Zodiaq
|