473,378 Members | 1,457 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,378 software developers and data experts.

2D arrays question

csx
Hi everyone!

two quick questions relating to arrays.

Q1,
Is it possible to re-assign array elements?

int array[2][2] = {{2,4}, {4,5}};

array[1][1] = {2,3}

just causes compiler errors! Do I have to assign at the beginning?

Q2,
My other 'main' question relates to 2-Dimensional arrays.

I need to create the size of my 2D array at run time. I have a couple of
functions that provide the dimensions I need. I need to call the function
like:

two_dimensional_array(maxRows, numLeafs)

but then, assign the values after, hence my first question. Can values be
asigned after?

Here is the code I currently have? Is this a good implementation? Any
suggestions would be much appreciated.

int ** myArray = 0; // in C++ 0==NULL

void two_dimensional_array(int rows, int cols)
{
int Rows, Cols;
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
myArray = new *int[Rows];
return 0;
}
Jul 19 '05 #1
7 11747
"csx" <cs*@ms.com> wrote in message
news:bl**********@sparta.btinternet.com...
Hi everyone!

two quick questions relating to arrays.

Q1,
Is it possible to re-assign array elements?
Yes, individually.

int array[2][2] = {{2,4}, {4,5}};

array[1][1] = {2,3}
Invalid syntax.

Also, array[1][1] denotes a single type 'int' object.
How do you expect to store two values in a single object?

Guessing at which elements you really want to change:

array[1][0] = 2;
array[1][1] = 3;

just causes compiler errors! Do I have to assign at the beginning?
You must assign each element's value individually.
If your values form an appropriate 'pattern' this
can often be done in a loop.
Q2,
My other 'main' question relates to 2-Dimensional arrays.

I need to create the size of my 2D array at run time. I have a couple of
functions that provide the dimensions I need. I need to call the function
like:

two_dimensional_array(maxRows, numLeafs)

but then, assign the values after, hence my first question. Can values be
asigned after?
Yes.
Here is the code I currently have? Is this a good implementation? Any
suggestions would be much appreciated.

int ** myArray = 0; // in C++ 0==NULL
This is not an array, it's a pointer. It does
not point to any storage you can use.
You must allocate some memory and assign its
address to the pointer. I see you tried to
below, but fall short.


void two_dimensional_array(int rows, int cols)
That's not a very desriptive name. I'd call it
e.g. 'load2darray', 'populate2darray' or something like that.
Also note that you never call this function in the code
you've posted.
{
int Rows, Cols;
Array dimensions should be stored in type 'size_t'
Type 'int' won't necessarily be able to represent
all possible array index values. 'size_t' is
guaranteed to be able to.

if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
}

int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char *argv[]) {

No "Windows-isms" allowed here.
myArray = new *int[Rows];
Invalid syntax.

myArray = new int*[Rows];

But this only allocates storage for 'Rows' pointers.
There's still no storage for them to point to.
return 0;
}


The difficulty so many have with arrays is one reason
for the invention of standard library containers such
as std::vector.

I recommend you use containers instead of arrays in
C++ programs, unless there's a compelling reaons to
do otherwise (e.g. interface with C or existing,
nonmodifiable code).

std::vector<std::vector> > arr2d;

-Can do anything an array can do, and more.
-Does all the memory management for you.
-Is 'flexible' -- will grow or shrink its size as needed.
-Carries its size with it, no need to pass an extra 'size'
to functions as with arrays.

If you insist upon using arrays, perhaps you'll find
useful an example in C I posted to comp.lang.c today. Look
for my name as author of a post in a thread entitled
"Re: How to free allocated memory?"

It uses the C functions 'malloc()' and 'free()' for
memory management, but you should be able to drop in
new[] and delete[] in their place -- you *must* do this
if you're creating arrays of types which have ctors/dtors,
or they won't get called.

But if you don't have to, don't use arrays. Use containers.
Really.

HTH,
-Mike
Jul 19 '05 #2
csx wrote:
Hi everyone!

two quick questions relating to arrays.

Q1,
Is it possible to re-assign array elements?
Of course, unless the elements are 'const'.

int array[2][2] = {{2,4}, {4,5}};

array[1][1] = {2,3}

just causes compiler errors! Do I have to assign at the beginning?
Of course it's an error. The {...} syntax is only for initializers. Why
are you trying to give one element two values anyway?

array[1][1] = 3;

Q2,
My other 'main' question relates to 2-Dimensional arrays.

I need to create the size of my 2D array at run time.
Say no more.

http://www.parashift.com/c++-faq-lit...html#faq-16.19
http://www.parashift.com/c++-faq-lit...html#faq-16.15

The short answer is: Use std::vector.
I have a couple of
functions that provide the dimensions I need. I need to call the function
like:

two_dimensional_array(maxRows, numLeafs)

but then, assign the values after, hence my first question. Can values be
asigned after?

Here is the code I currently have? Is this a good implementation? Any
suggestions would be much appreciated.

int ** myArray = 0; // in C++ 0==NULL
Evil global variable.

void two_dimensional_array(int rows, int cols)
{
int Rows, Cols;
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
}
Why wouldn't you have this function return the pointer rather than
acting on an evil global variable?

Besides that, it seems really broken. Why doesn't it allocate the
initial int* array? Why does it indiscriminately overwrite the elements
of the array that myArray points to? What if those pointers already
pointed to dynamic memory?

int _tmain(int argc, _TCHAR* argv[])
_tmain is a reserved identifier in the global namespace (as are all
identifiers beginning with an underscore followed by a lower-case
letter). That means you may not use it as a name in the global
namespace. Also, _TCHAR is reserved in all contexts (as are all
identifiers beginning with an underscore followed by an upper-case
letter or another underscore), and you may not use it at all. In
general, avoid identifier names beginning with an underscore. (And try
posting *standard* C++ code when you post here.)
{
myArray = new *int[Rows];
return 0;
Uh, memory leak? What exactly was this supposed to demonstrate?
}


-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
Mike Wahler wrote:


The difficulty so many have with arrays is one reason
for the invention of standard library containers such
as std::vector.

I recommend you use containers instead of arrays in
C++ programs, unless there's a compelling reaons to
do otherwise (e.g. interface with C or existing,
nonmodifiable code).

std::vector<std::vector> > arr2d;


std::vector<std::vector<int> > arr2d;

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:7R*****************@newsread3.news.pas.earthl ink.net...
Mike Wahler wrote:
std::vector<std::vector> > arr2d;


std::vector<std::vector<int> > arr2d;


Oops, yes, thanks.

-Mike
Jul 19 '05 #5
csx
Hi, and thanks so much! Ive looked at your array example in C, and it has
been very helpful.
However, there were a couple of things. In this function:

double **alloc2d(size_t rows, size_t cols)
{
double **result;
size_t row = 0;
size_t col = 0;
if(result = malloc(rows * sizeof *result))
{
if(*result = malloc(rows * cols * sizeof **result)
for(row = 1; row < rows; ++row)
result[row] = *result + row * cols;
else
{
free(result);
result = NULL;
}
}
return result;
}
There is a problem on the lines
if(result = malloc(rows * sizeof *result))
if(*result = malloc(rows * cols * sizeof **result)

There are the errors:
error C2440: '=' : cannot convert from 'void *' to 'double ** '
error C2440: '=' : cannot convert from 'void *' to 'double *'

Not sure how to fix these, can anyone provide advise?
Many 'Thanks' in advance!!
Jul 19 '05 #6
"csx" <cs*@ms.com> wrote in message
news:bl**********@hercules.btinternet.com...
Hi, and thanks so much! Ive looked at your array example in C, and it has
been very helpful.
First note that C is not C++. They look very similar
in syntax, the the semantics are different in many
cases. You've just discovered one example of this.

I referred you to that C code for ideas, not
as a 'drop in' C++ solution to your questions.

However, that code I wrote can be made valid C++ with
a simple 'fix'. (actually I'd call it a 'hack').

Or you can configure your compiler to compile
C instead of C++).

See below.
However, there were a couple of things. In this function:

double **alloc2d(size_t rows, size_t cols)
{
double **result;
size_t row = 0;
size_t col = 0;
if(result = malloc(rows * sizeof *result))
{
if(*result = malloc(rows * cols * sizeof **result)
for(row = 1; row < rows; ++row)
result[row] = *result + row * cols;
else
{
free(result);
result = NULL;
}
}
return result;
}
There is a problem on the lines
if(result = malloc(rows * sizeof *result))
if(*result = malloc(rows * cols * sizeof **result)

There are the errors:
error C2440: '=' : cannot convert from 'void *' to 'double ** '
error C2440: '=' : cannot convert from 'void *' to 'double *'
Those are indeed errors in C++, but not in C.
Not sure how to fix these, can anyone provide advise?
Many 'Thanks' in advance!!


Remember what I wrote:

"It uses the C functions 'malloc()' and 'free()' for
memory management, but you should be able to drop in
new[] and delete[] in their place -- you *must* do this
if you're creating arrays of types which have ctors/dtors,
or they won't get called."

I forgot to add "if you use 'malloc()' you'll need to
cast the return value to the target type"

C allows the conversion from/to 'void*' and any other
pointer type without a cast. But C++ requires a cast:

/* hack: */
if(result = (double**)malloc(rows * sizeof *result))
if(*result = (double*)malloc(rows * cols * sizeof **result)

I also wrote:
"But if you don't have to, don't use arrays. Use containers.
Really."

And I still stand by that advice.

-Mike
Jul 19 '05 #7
csx
Thanks Mike!
Jul 19 '05 #8

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

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
12
by: Samee Zahur | last post by:
Back in the days of old C, only numeric literals could be used as dimensions for statically allocated arrays - the size had to be resolved to a constant at/before compile time. Now I'm beginning to...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: rir3760 | last post by:
Since a few days ago I have been working with the program I post below (a school assignment). The purpose of the program is to work with the va_ macros (stdarg.h) and arrays of arrays, hopefully...
15
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
3
by: James dean | last post by:
I have created algorithms in C# unsafe code and have fixed the arrays in memory for optimum performance. I use multidimensional arrays rather than jagged arrays. The algorithms i use usually read a...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.