Re: [Dev-C++] copying elements to an array of pointers...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2002-10-27 14:40:14
|
But most proably that wasn't the intention.
The fact that both array and string was sized with max must mean
something... in this case a failed attempt at space for the characters. By
mistake, the declaration instead created a number of pointers.
char *ptr;
char string[max];
ptr = new char[max];
strncpy(ptr,string,max);
ptr[max-1] = '\0';
or instead
string[max-1] = '\0';
ptr = strdup(string);
/Per W
On Sun, 27 Oct 2002, Abhijit Shylanath wrote:
> > got a question:
> > i'd like to copy a char string to a string of pointers but i can't get the
> > code working...and i can't see the result with cout... anyone who can help
> > me??
> > kind regards,
> > senem
>
> > #include <iostream.h>
> > #include <stdlib.h>
> > #include <string.h>
> > #include <stdio.h>
> > #define max 5
> > int main(){
> > char *array[max];
>
> Here, you've just declared an array of pointers. You haven't actually
> allocated memory, so the pointers are not valid. You need to new[] it, like
> so.
>
> array[0] = new char[max];
>
> and so on.
>
> > char string[max];
> > cout<<"enter string :";
> > cin.getline(string,max);
> > strncpy(array[0],string,sizeof(string)/sizeof(char));
> > cout<<array[0];
> > return 0;
> > }
>
> ___________________________________________________________
>
> Abhijit Shylanath
>
> E-mail: mu...@gm... || ibr...@bi...
> Web-site: http://mudeth.tripod.com/
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ApacheCon, November 18-21 in
> Las Vegas (supported by COMDEX), the only Apache event to be
> fully supported by the ASF. http://www.apachecon.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
>
|