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

Question about arrays and pointers, please help

7
I came across this when I was reading a book on Java.

In the book, there is a note trying to explain what's equivalent in C++ to

Expand|Select|Wrap|Line Numbers
  1. double[][] balance = new double[10][6];  // Java
So my question is what's the difference between

Expand|Select|Wrap|Line Numbers
  1. double balance[10][6];
and
Expand|Select|Wrap|Line Numbers
  1. double (*balance)[6] = new double[10][6];
and

Expand|Select|Wrap|Line Numbers
  1. double** balance = new double* [10];

I am learning c++ without a teacher and have no where to turn to. Arrays and pointers are getting very confusing. Your help is highly appreciated.
Dec 13 '09 #1

✓ answered by weaknessforcats


7 1489
double balance[10][6];

Its an array oy type double with dimension 10X6


double (*balance)[6] = new double[10][6];

Its an array of pointers which points to array of type double with dimension 10X6.


double** balance = new double* [10];

Balance is a pointer to pointer which points to an array of pointers of size 10.
Dec 13 '09 #2
Banfa
9,065 Expert Mod 8TB
double (*balance)[6] = new double[10][6];

Its an array of pointers which points to array of type double with dimension 10X6.
double (*balance)[6];

is not an array of pointers, it is a pointer to an array of size 6. The pointer to the array of size 6 is pointed to an array of size 10 of arrays of size 6.

An array of pointers would be

double *balance[6];

</pedantic>
Dec 13 '09 #3
@Banfa

You are right
Dec 13 '09 #4
QiongZ
7
Thanks everyone and what a great post, weaknessforcats!

Now I understood

double (*balance)[6] = new double[10][6];

but still don't know what

Expand|Select|Wrap|Line Numbers
  1. double** balance = new double* [10];
means in C++?

In your article, you said

Expand|Select|Wrap|Line Numbers
  1. double** balance = new double [10][6];
would be error.

Is

Expand|Select|Wrap|Line Numbers
  1. new double [10][6];
equivalent to

Expand|Select|Wrap|Line Numbers
  1. new double* [10];
?

Thanks very much!
Dec 13 '09 #6
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. double** balance = new double* [10];
balance is a pointer to a pointer to a double. This is actually a very basic array assignment. If you want a dynamically allocated array of some abstract type T then you declare a pointer to T and new an array of T like so

T *array = new T[<dimension>];

In this case T is double *, a pointer to double (and <dimension> is 10) giving

double **array = new double*[10];

Just the same as allocating a normal int array, setting T to int gives

int *array = new int[<dimension>];

new double [10][6];

is in no way equivalent to

new double* [10];

The first allocates a 2 dimensioned array of doubles, or rather an array of arrays of doubles. The second allocates an array of pointers to double. Very very different.
Dec 13 '09 #7
QiongZ
7
Thanks a lot Banfa for taking time to explain in detail. Also you explained it in a way very easy to understand.

Every time I post here, I get excellent answers. I can't thank you guys enough.

Qiong
Dec 14 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
6
by: joelperr | last post by:
Hello, I am attempting to separate a two dimensional array into two one-dimensional arrays through a function. The goal of this is that, from the rest of the program, a data file consisting of...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
73
by: JoeC | last post by:
I am writing a game and I am having a challenge with my combat function. All I want to do is find out how to group pieces that are in the same space. There are two sides and all the units that...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
6
by: jason | last post by:
Hello, I have a question about what kind of datastructure to use. I'm reading collumn based data in the form of: 10\t12\t9\t11\n 24\t11\t4\t10\n ..... I now have a structure which allows me...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.