5 Replies - 4271 Views - Last Post: 02 May 2009 - 08:02 PM Rate Topic: -----

#1 r_a_v_i_1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 01-May 09

Declaring and initializing an array of pointers inside a class

Post icon  Posted 01 May 2009 - 08:27 AM

Can any one guide me to declare an array of char pointers and initializing at one shot
Is This A Good Question/Topic? 0
  • +

Replies To: Declaring and initializing an array of pointers inside a class

#2 KYA  Icon User is offline

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

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Declaring and initializing an array of pointers inside a class

Posted 01 May 2009 - 08:39 AM

char** charArr = new char*[numCharsYouWant];

//allocate memory for each pointer in the array
//do stuff
//delete/free each individual pointer
delete[] charArr;


Was This Post Helpful? 0
  • +
  • -

#3 r_a_v_i_1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 01-May 09

Re: Declaring and initializing an array of pointers inside a class

Posted 02 May 2009 - 09:13 AM

View PostKYA, on 1 May, 2009 - 07:39 AM, said:

char** charArr = new char*[numCharsYouWant];

//allocate memory for each pointer in the array
//do stuff
//delete/free each individual pointer
delete[] charArr;



i tried this but it did not not work.this can be done outside the class but when this technique is applied inside the class the compiler throws error .please can u demonstrate this with an example inside the class
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

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

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Declaring and initializing an array of pointers inside a class

Posted 02 May 2009 - 09:16 AM

 
class Example {
private:
   char** charArr;
public:
	 Example(int n)								{charArr = new char*[n];};
	 ~Example()									 {delete[] charrArr;};
};



You'll also need to free any allocated memory per pointer in the array
Was This Post Helpful? 0
  • +
  • -

#5 programmer_temae  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 01-May 09

Re: Declaring and initializing an array of pointers inside a class

Posted 02 May 2009 - 07:46 PM

View PostKYA, on 2 May, 2009 - 08:16 AM, said:

 
class Example {
private:
   char** charArr;
public:
	 Example(int n)								{charArr = new char*[n];};
	 ~Example()									 {delete[] charrArr;};
};



You'll also need to free any allocated memory per pointer in the array


hi, when an object is instanciated the pointer to pointer is pointing to the first string? and which is the size of each string?
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

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

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Declaring and initializing an array of pointers inside a class

Posted 02 May 2009 - 08:02 PM

They wanted an array of pointers so a ** is a pointer to a pointer. In the constructor we simply make an array of pointers of size 'n'. You will then have to determine the size of each string then and free it accordingly.

This post has been edited by KYA: 02 May 2009 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1