Re: [Dev-C++] copying elements to an array of pointers...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Abhijit S. <mu...@gm...> - 2002-10-27 13:33:43
|
> 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/
|