| | |
Array problems
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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
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
something like this will hold an unlimited number of strings that are of unlimited length.
C++ Syntax (Toggle Plain Text)
const char *array[] = { "jane", "John Deer Company", "The quick brown fox jumped over the lazy dog", "Adam", "Smith" };
•
•
•
•
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?
"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
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.
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
•
•
•
•
Originally Posted by Dave Sinkula
What is your intent -- what issue are you trying to solve?
[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?
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
[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?
#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?
you can use malloc() to allocate the space needed to hold the characters
C++ Syntax (Toggle Plain Text)
char *array2 = malloc( strlen(array[0]) +1 ); strcpy(array2, array[0]);
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
•
•
•
•
Originally Posted by Ancient Dragon
you can use malloc() to allocate the space needed to hold the characters
C++ Syntax (Toggle Plain Text)
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)
[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]
malloc() must be typecast when using a c++ compiler (file extension is *.cpp or *.cc), C compilers do not have that restriction.
Since you are writing c++ code, if you have the choice use std::string instead -- its a lot simpler.
C++ Syntax (Toggle Plain Text)
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)
std::string array2 = array[0];
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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?
[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?
![]() |
Similar Threads
- Array problems (C++)
- array problems (C++)
- Problem installing XP on a SATA Striping Array (Storage)
- error 88:'(' expected when trying to display an array any help (Pascal and Delphi)
Other Threads in the C++ Forum
- Previous Thread: stack palindrome problem?
- Next Thread: Newbie to C++
Views: 4905 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ vector video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






