473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Two dimensional array problem

Hi,

I've got problems with 2D array.

I have a program that gets results from sql server 2000 and
puts the results in an std::string.
Here's a few example lines of code to help explain my problem.

<--- myprog.h --->
#define MAXROWS 200
#define MAXCOLUMNS 100

std::string pszResultValues[MAXROWS][MAXCOLUMNS];
<--- myprog.h --->

<--- myprog.cpp --->
long row=0,column=0;
pszResultValues[row][column]=_bstr_t(pszReceivedData);
<--- myprog.cpp --->

I loop through the results and add them to pszResultValues.
It works fine MAXROWS isn't bigger than 200 and MAXCOLUMNS
isn't bigger than 100. If I put MAXCOLUMNS value past 100
it fails. Is that some sort of array restriction or what ?
Is there a way to somehow define dynamic values to
MAXROWS and MAXCOLUMNS or is there some other solution.

I would post the actual code that I use, but it uses
companys own API etc stuff so I can't post them here.
I hope you understand my dilemma from the above.

Thanks in advance for all the help.

----
mkarja
Jul 22 '05 #1
5 2958
"mkarja" <mm**********@hotmail.com> wrote in message
news:75*************************@posting.google.co m...
Hi,

I've got problems with 2D array.

I have a program that gets results from sql server 2000 and
puts the results in an std::string.
Here's a few example lines of code to help explain my problem.

<--- myprog.h --->
#define MAXROWS 200
#define MAXCOLUMNS 100

std::string pszResultValues[MAXROWS][MAXCOLUMNS];
<--- myprog.h --->

<--- myprog.cpp --->
long row=0,column=0;
pszResultValues[row][column]=_bstr_t(pszReceivedData);
<--- myprog.cpp --->

I loop through the results and add them to pszResultValues.
It works fine MAXROWS isn't bigger than 200 and MAXCOLUMNS
isn't bigger than 100. If I put MAXCOLUMNS value past 100
it fails. Is that some sort of array restriction or what ?
Is there a way to somehow define dynamic values to
MAXROWS and MAXCOLUMNS or is there some other solution.

I would post the actual code that I use, but it uses
companys own API etc stuff so I can't post them here.
I hope you understand my dilemma from the above.

Thanks in advance for all the help.

----
mkarja


int maxrows = 4;

int maxcols = 1;

using std::vector;

using std::string;

vector< vector<string> > v(maxrows, vector<string>(maxcols));

v[yourCol][yourRow] = yourString;

You can now increase either the row or col dynamically as:

// increase by one row, X cols:

v.push_back(vector<string>(X));

// increase row X by 1 col:

v[X].push_back(string("hello"));

HTH,

Elias
Jul 22 '05 #2
In article <75*************************@posting.google.com> ,
mkarja <mm**********@hotmail.com> wrote:

<--- myprog.h --->
#define MAXROWS 200
#define MAXCOLUMNS 100

std::string pszResultValues[MAXROWS][MAXCOLUMNS];
<--- myprog.h --->

<--- myprog.cpp --->
long row=0,column=0;
pszResultValues[row][column]=_bstr_t(pszReceivedData);
<--- myprog.cpp --->

I loop through the results and add them to pszResultValues.
It works fine MAXROWS isn't bigger than 200 and MAXCOLUMNS
isn't bigger than 100. If I put MAXCOLUMNS value past 100
it fails. Is that some sort of array restriction or what ?
There's no fundamental restriction (in the C++ language), but your
compiler or operating system may have a limit on how memory you can
allocate for an "automatic" variable (often called "on the stack").
Is there a way to somehow define dynamic values to
MAXROWS and MAXCOLUMNS or is there some other solution.


Try using a vector of vectors instead of a 2d array. Vectors allocate
their data memory dynamically, i.e. not "on the stack".

std::vector<std::vector<std::string> > pszResultValues (MAXROWS,
std::vector<std::string>(MAXCOLUMNS));

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #3
Maybe you cross the memory limit?

Try using pointers... Not sure if they work for string, but I'm sure
they do for integers, etc.

example:

int **some_array = new int*[how_many_rows];
//how_many_rows can be a variable

for (int i = 0; i < how_many_rows; i++) {
some_array[i] = new int[how_many_columns];
//how_many_columns can be a variable as well
for (int j = 0; j < how_many_columns; j++)
//INITIALISE HERE
}

---

cmad
Jul 22 '05 #4
Sorry, didn't tell remind you that you have to deallocate the space...

and the end of your program do:

for (int k = 0; k < how_many_rows; k++)
delete [] some_array[k];

delete [] some_array;

---

cmad
Jul 22 '05 #5
Thanks, for the help.
But now there's another problem. I can't get the result of the
sql search in the vector.
It's a char pointer like this char* pszReceivedData=NULL;
It gives me error message that is about mile long.
error C2109: subscript requires array or pointer type
error C2109: subscript requires array or pointer type
error C2440: '=' : cannot convert from 'char *' to
'class std::vector<class std::vector<class std::basic_string<char,
struct std::char_traits<char>,class std::allocator<char> >,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > >,class
std::allocator<class std::vector<class std::basic_string<char,
struct std::char_traits<char>,class std::allocator<char> >,
class std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > > > >
(__thiscall CexecuteSearch::*)(void)'

What / how do I have to convert the result to make it work ?

----
mkarja
jt*******@presby.edu (Jon Bell) wrote in message news:<br**********@jtbell.presby.edu>...
In article <75*************************@posting.google.com> ,
mkarja <mm**********@hotmail.com> wrote:

<--- myprog.h --->
#define MAXROWS 200
#define MAXCOLUMNS 100

std::string pszResultValues[MAXROWS][MAXCOLUMNS];
<--- myprog.h --->

<--- myprog.cpp --->
long row=0,column=0;
pszResultValues[row][column]=_bstr_t(pszReceivedData);
<--- myprog.cpp --->

I loop through the results and add them to pszResultValues.
It works fine MAXROWS isn't bigger than 200 and MAXCOLUMNS
isn't bigger than 100. If I put MAXCOLUMNS value past 100
it fails. Is that some sort of array restriction or what ?


There's no fundamental restriction (in the C++ language), but your
compiler or operating system may have a limit on how memory you can
allocate for an "automatic" variable (often called "on the stack").
Is there a way to somehow define dynamic values to
MAXROWS and MAXCOLUMNS or is there some other solution.


Try using a vector of vectors instead of a 2d array. Vectors allocate
their data memory dynamically, i.e. not "on the stack".

std::vector<std::vector<std::string> > pszResultValues (MAXROWS,
std::vector<std::string>(MAXCOLUMNS));

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: Todd | last post by:
I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are...
20
by: Parrot | last post by:
I am trying to program a function to return a 2 dimensional array, but it's not working. I reduced the return value to 1 dimension and tested that to make sure that the problem wasn't elsewhere. ...
6
by: Ruben | last post by:
I'm trying to pass an array of string to a function without knowing how many strings I have beforehand. I've defined one functions as char * insert(char table,int cols, char values); out of...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
6
by: fniles | last post by:
I need to store information in a 2 dimensional array. I understand ArrayList only works for a single dimensional array, is that correct ? So, I use the 2 dimensional array like in VB6. I pass the...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
6
by: hyena | last post by:
Hi, I have a problem regarding passin 2 dimensional array into a function. I have a to pass into function f(), f is called many times and the size of a will change for each call. I am not...
5
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.