7 Replies - 258 Views - Last Post: 02 February 2013 - 09:22 AM Rate Topic: -----

#1 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

Object array

Posted 02 February 2013 - 05:26 AM

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Object array

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

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.
Was This Post Helpful? 0
  • +
  • -

#3 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

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!
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Object array

Posted 02 February 2013 - 06:48 AM

View Postsanwall, 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?
Was This Post Helpful? 0
  • +
  • -

#5 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

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??
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

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
Was This Post Helpful? 1
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Object array

Posted 02 February 2013 - 09:03 AM

View Postsanwall, 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??


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.
Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1