Not a problem as such but am i asking too much....heres the current situation:

I've got a multi -dimension array populated........

char buffer[5]; // holds name
	char holdingArray[5][5] = //holds populating array , copies to buffer
	{
		"Jame", 
		"Anna", 
		"heya",
		"rand"
	};

ok as you can see the array cant be anymore than 4 letters in lenght. I've looked in dynamic memory allocation...I can do a dynamic array for a 'int' but not for a multi dimensional array, can anyone help us out here?

thanks

something like this will hold an unlimited number of strings that are of unlimited length.

const char *array[] = {
 "jane",
 "John Deer Company",
 "The quick brown fox jumped over the lazy dog",
 "Adam",
 "Smith"
};

Not a problem as such but am i asking too much....

I can do a dynamic array for a 'int' but not for a multi dimensional array, can anyone help us out here?

What is your intent -- what issue are you trying to solve?

Dynamic allocation of arrays of more than one dimension is not so easily done, because dynamic allocation of an n-dimensional array actually requires dynamic allocation of n 1-dimensional arrays. To allocate a 2-dimensional array you must first allocate memory sufficient to hold all the elements of the array (the data), then allocate memory for pointers to each row of the array. For arrays of more than two dimensions it gets more complicated. To allocate a 3-dimensional array you first allocate memory for the data, then allocate memory for an array of pointers to rows of data within that memory, then allocate memory for an array of pointers to a subset of those pointers. And so on for arrays of higher dimension.

What is your intent -- what issue are you trying to solve?

Basically I've got a random seeder, this helps to select a word from that multi di - array and copy that into the buffer.

char buffer[5]; // holds name
	char holdingArray[5][5] = //holds populating array , copies to buffer
	{
		"Jame", 
                          "Anna", 
                          "heya", 
                          "rand" 

	};

srand(time(0)) ; 
int random;

random = ( rand() % 4);
   strcpy(buffer, holdingArray[random]);

then the buffer is copied to a class data attribute via the constructor. But what if we didnt know the size of the word? What if I wanted to have a mixture of lenght of words? My question stands which is the best way to make an array of this type without knowing the word size yet still been able to pass it into a constructor ready for use?

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	int i=15;
char array2[15];
const char *array[] = {
 "jane",
 "John Deer Company",
 "The quick brown fox jumped over the lazy dog",
 "Adam",
 "Smith"
};

cout <<array[0];

strcpy(array2,array[2]);

cout << array2;

return 0;
}

ok after playing around in order for the biggest word to be placed in the buffer , the buffer must be set to the maxium word size. I'm wondering if i could dynamically get that since really an array of [30] holiding 4 chars and a null is a waste?

you can use malloc() to allocate the space needed to hold the characters

char *array2 = malloc( strlen(array[0]) +1 );
strcpy(array2, array[0]);

you can use malloc() to allocate the space needed to hold the characters

char *array2 = malloc( strlen(array[0]) +1 );
strcpy(array2, array[0]);

Compiling...
Copy of dynamic memory.cpp
C:\Documents and Settings\John Rudd\Desktop\hangman\dynaic memory\Copy of dynamic memory.cpp(19) : error C2440: 'initializing' : cannot convert from 'void *' to 'char *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Error executing cl.exe.

Copy of dynamic memory.obj - 1 error(s), 0 warning(s)

ok this doesnt compile

#include <iostream>
#include <cstring>

using namespace std;

int main()
{



const char *array[] = {
 "jane",
 "John Deer Company",
 "The quick brown fox jumped over the lazy dog",
 "Adam",
 "Smith"
};

char *array2 = malloc( strlen(array[0]) +1 );

cout <<array[0];

strcpy(array2,array[2]);




cout << array2;

return 0;
}

malloc() must be typecast when using a c++ compiler (file extension is *.cpp or *.cc), C compilers do not have that restriction.

char *array2 = reinterpret_cast<char*>(malloc( strlen(array[0]) +1 ));

Since you are writing c++ code, if you have the choice use std::string instead -- its a lot simpler.

std::string array2 = array[0];

fair enough.... Ive got this now....but I'm left puzzled....

#include <iostream>
#include <cstring>
#include <ctime>

using namespace std;

int main()
{

srand(time(0)) ; 
int random;

random = ( rand() % 4);

  char *buffer ;
  char *array2;
const char *array[] = {
 "jane",
 "John Deer Company",
 "The quick brown fox jumped over the lazy dog",
 "Adam",
 "Smith"
};
	
array2 = new char[ strlen( array[random] ) + 1 ];

strcpy(array2,array[random]);

cout << array2;

buffer = new char[ strlen(array2)];

cout << "string lenght of array 2 is:" <<strlen(array2);
cout << "lenght of buffer is" <<strlen(buffer);

delete [] array2;
delete [] buffer;


return 0;
}

why is it that the 2 outputs are not the same lenght? array2 sometimes equals 4 which is fair enough, but when that occurs buffer = 9!! how can that be?

You assign memory to buffer with the new statement which makes it capable of holding a string but you don't actually assign a string to buffer. Therefore buffer is empty, meaning it isn't a null terminated char array. I'm not sure what behaviour to expect when you send an empty char array to strlen which expects a null terminated char array. I suspect it is undefined behaviour and you compiler is returning a random value of 9.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.