2 Replies - 1597 Views - Last Post: 05 January 2011 - 11:29 AM Rate Topic: -----

#1 Guest_Aequitas1*


Reputation:

Copying the contents of a Char array to another char array.

Posted 05 January 2011 - 10:57 AM

Hey there everyone, I'm attempting to copy the contents of a char array to another char array. The issue is the array I'm attempting to copy the string literal to is just a bad pointer. I have no idea how to proceed! Here is a sample of my constructor that I'm using to do this:

	ListNode(const char* elements) : mPrev(0), mNext(0)
	{
		//Get the length of the string input into the constructor.
		int length = strlen(elements);
		//Assign enough memory to mNode to copy across the string.
		mNode = new char[length];
		//Copy across the new string.
		strcpy(mNode, elements);
	}


Is This A Good Question/Topic? 0

Replies To: Copying the contents of a Char array to another char array.

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,165
  • Joined: 14-September 07

Re: Copying the contents of a Char array to another char array.

Posted 05 January 2011 - 11:01 AM

You need to leave space for the null terminator: length + 1.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1767
  • View blog
  • Posts: 3,424
  • Joined: 30-May 10

Re: Copying the contents of a Char array to another char array.

Posted 05 January 2011 - 11:29 AM

> I have no idea how to proceed!
This is C++, so use a std::string and get on with life.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1