473,698 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about arrays and pointers, please help

7 New Member
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
7 1499
puneetsardana88
57 New Member
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 Recognized Expert Moderator Expert
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
puneetsardana88
57 New Member
@Banfa

You are right
Dec 13 '09 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
Read this:

http://bytes.com/topic/c/insights/77...rrays-revealed
Dec 13 '09 #5
QiongZ
7 New Member
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 Recognized Expert Moderator Expert
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 New Member
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
3925
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 "browse" it). So I expected that when I run this program, I get both c1.A and c2.A pointing to the same address, and changing c1.A means that also c2.A changes too. ----- BEGIN example CODE -----------
11
6752
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 same type). eg: int arr1;//Array of 20 ints int arr2; int arr3; ............
35
2719
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 I'm pretty sure the rest of the program is working just fine. The only thing I could think might be wrong is that the if statement can only hold so many values in itself? Let me show what I'm doing: if (table001]>>5]&b&0x1f] != 0 &&
8
2246
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 that a declaration like: int intArray means that there is an array of pointers to int with a size of 5, each of whose elements is an array of int with a size of 3. This definition seemed intuitive to me since a declaration like
6
491
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 two columns of coordinate data is read and stored into a 2D array. From this 2D array I would like to split it into two 1D arrays, consisting of x and y components, respectively. However, to return the two arrays from the function I am...
24
3446
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
73
4278
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 are in the same space fight. I want to add up the attack factors and defending factors in the same space then figure out the odds so I can roll against an odds table. Basically each piece holds its own x and y loc. Here is what I have right...
4
2502
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
1666
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 to access the data like this: x->row.coll.value.d;
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9023
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7721
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.