942,958 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6974
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 14th, 2005
0

Array problems

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Sep 14th, 2005
0

Re: Array problems

something like this will hold an unlimited number of strings that are of unlimited length.
C++ Syntax (Toggle Plain Text)
  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. };
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5591
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,930 posts
since Aug 2005
Sep 14th, 2005
0

Re: Array problems

Quote 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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 15th, 2005
0

Re: Array problems

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.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Sep 15th, 2005
0

Re: Array problems

Quote 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?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Sep 15th, 2005
0

Re: Array problems

[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?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Sep 15th, 2005
0

Re: Array problems

you can use malloc() to allocate the space needed to hold the characters
C++ Syntax (Toggle Plain Text)
  1. char *array2 = malloc( strlen(array[0]) +1 );
  2. strcpy(array2, array[0]);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5591
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,930 posts
since Aug 2005
Sep 15th, 2005
0

Re: Array problems

Quote originally posted by Ancient Dragon ...
you can use malloc() to allocate the space needed to hold the characters
C++ Syntax (Toggle Plain Text)
  1. char *array2 = malloc( strlen(array[0]) +1 );
  2. strcpy(array2, array[0]);

Quote ...
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]
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Sep 15th, 2005
0

Re: Array problems

malloc() must be typecast when using a c++ compiler (file extension is *.cpp or *.cc), C compilers do not have that restriction.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. std::string array2 = array[0];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5591
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,930 posts
since Aug 2005
Sep 15th, 2005
0

Re: Array problems

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?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: stack palindrome problem?
Next Thread in C++ Forum Timeline: Newbie to C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC