using namespace std;
class book
{
private:
char *author[3];
public:
book(char *a)
{
for(int i=0;i<=2;i++)
{
strcpy(author[i],a);
cout<<*(author+i)<<endl;
}
}
};
int main()
{
book b[3]={book("Hi 2 all"),book("Have fun"),book("Bad boy")};
getch();
return 0;
}
Object array
Page 1 of 17 Replies - 156 Views - Last Post: 02 February 2013 - 09:22 AM
#1
Object array
Posted 02 February 2013 - 05:26 AM
Replies To: Object array
#2
Re: Object array
Posted 02 February 2013 - 05:35 AM
What's your question?
PS: You shouldn't use C string functions like strcpy in C++ - using std::strings is much easier and safer, your usage of strcpy invokes undefined behavior because you didn't initialize the pointers that you're using as the target, and you didn't include any headers, so you're using a lot of undeclared values and functions.
PS: You shouldn't use C string functions like strcpy in C++ - using std::strings is much easier and safer, your usage of strcpy invokes undefined behavior because you didn't initialize the pointers that you're using as the target, and you didn't include any headers, so you're using a lot of undeclared values and functions.
#3
Re: Object array
Posted 02 February 2013 - 06:35 AM
Dear my question is why the program just crashes? And I want only the output!
#4
Re: Object array
Posted 02 February 2013 - 06:48 AM
sanwall, on 02 February 2013 - 02:35 PM, said:
Dear my question is why the program just crashes?
I think I answered that in my postscript: you're strcpying into a pointer that you did not initialize. That may very well cause a crash.
Though I'm a bit surprised that your compiler even compiles the code despite the missing includes.
Quote
And I want only the output!
What do you mean by that?
#5
Re: Object array
Posted 02 February 2013 - 08:29 AM
'*a' pointer is intialized by "Hi 2 all" address which will be stored in author[0] by using strcpy!!!
. Is not it??
. Is not it??
#6
Re: Object array
Posted 02 February 2013 - 08:52 AM
No strcpy() doesn't allocate memory, it requires a pointer to an array previously allocated memory.
Jim
Jim
#7
Re: Object array
Posted 02 February 2013 - 09:03 AM
sanwall, on 02 February 2013 - 04:29 PM, said:
'*a' pointer is intialized by "Hi 2 all" address which will be stored in author[0] by using strcpy!!!
. Is not it??
. Is not it??
a is initialized, but author[i] is not. As jimblumberg says, you need to allocate memory of the necessary size and make author[i] point to it before you can copy anything into it using strcpy.
And as I previously said, you can avoid that whole headache by using std::strings.
#8
Re: Object array
Posted 02 February 2013 - 09:22 AM
It really doesn't make much sense to copy the same name to your array either. Normally a book has one author or several different authors, but not the same one. Also why are you trying to copy a C-string that looks like a book title to your author variable?
You may want to look at this link: Arrays.
As already stated you should really consider using a std::string instead of your array. But if you must use arrays then maybe you should consider using a statically allocated array author[3][30];.
Jim
You may want to look at this link: Arrays.
As already stated you should really consider using a std::string instead of your array. But if you must use arrays then maybe you should consider using a statically allocated array author[3][30];.
Jim
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|