Array problems

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Array problems

 
0
  #1
Sep 14th, 2005
Not a problem as such but am i asking too much....heres the current situation:

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

[php]

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

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,163
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Array problems

 
0
  #2
Sep 14th, 2005
something like this will hold an unlimited number of strings that are of unlimited length.
  1. const char *array[] = {
  2. "jane",
  3. "John Deer Company",
  4. "The quick brown fox jumped over the lazy dog",
  5. "Adam",
  6. "Smith"
  7. };
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,613
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 278
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array problems

 
0
  #3
Sep 14th, 2005
Originally Posted by Acidburn
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Array problems

 
0
  #4
Sep 15th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Array problems

 
0
  #5
Sep 15th, 2005
Originally Posted by Dave Sinkula
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.

[php]
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]);
[/php]

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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Array problems

 
0
  #6
Sep 15th, 2005
[php]#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;
}

[/php]

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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,163
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Array problems

 
0
  #7
Sep 15th, 2005
you can use malloc() to allocate the space needed to hold the characters
  1. char *array2 = malloc( strlen(array[0]) +1 );
  2. strcpy(array2, array[0]);
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Array problems

 
0
  #8
Sep 15th, 2005
Originally Posted by Ancient Dragon
you can use malloc() to allocate the space needed to hold the characters
  1. char *array2 = malloc( strlen(array[0]) +1 );
  2. 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

[php]
#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;
}
[/php]
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,163
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Array problems

 
0
  #9
Sep 15th, 2005
malloc() must be typecast when using a c++ compiler (file extension is *.cpp or *.cc), C compilers do not have that restriction.
  1. 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.
  1. std::string array2 = array[0];
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Array problems

 
0
  #10
Sep 15th, 2005
fair enough.... Ive got this now....but I'm left puzzled....
[php]
#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;
}
[/php]

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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4905 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC