473,327 Members | 2,118 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,327 software developers and data experts.

Simple question: Cannot convert 2D array to pointer-to-pointer

I can do this:

int asdf[2];
int* zxcv = asdf;

but not this:

int asdf[2][2];
int** zxcv = asdf;

or I get 'cannot convert from int[2][2] to int**'. Does anybody know why
this is, and how to get around it? I just want a pointer to that double
array. Thanks in advance.
Jul 22 '05 #1
5 3152

"overbored" <ov*********@SPAMoverbored.net> wrote in message
news:Xn******************************@127.0.0.1...
I can do this:

int asdf[2];
int* zxcv = asdf;

but not this:

int asdf[2][2];
int** zxcv = asdf;

or I get 'cannot convert from int[2][2] to int**'. Does anybody know why
this is, and how to get around it?
C++ says T[] converts to T*, it doesn't say anything about T[][] to T** and
why should it? It would be impossible to implement. T[] converts to T*
because x[i] is equivalent to *(x + i). But x[i][j] is not equivalent to
*(*(x + i) + j) because the pointer arithmetic doesn't work.
I just want a pointer to that double
array. Thanks in advance.


Why? If you explain what you are trying to do then maybe someone can tell
you how.

You can do this

int asdf[2][2];
int (*zxcv)[2] = asdf;

Is that good enough?

john
Jul 22 '05 #2
I don't understand your explanation. Why do you say the pointer
arithmetic "wouldn't work"? Assuming x : int**,

x
+---+ +---+ +---+---+
| |---->| |---->| 5 | 7 |
+---+ +---+ +---+---+
| |
+---+
int** int* int
So,

x : int** returns the address of the 0th element of the first array.

x + i : int** returns the address of the ith element of the first array.

*(x + i) : int* returns the value of the ith element of the first array,
or the address of the 0th element in the second array.

*(x + i) + j : int* returns the address of the jth element in the second
array.

*(*(x + i) + j) : int returns the value of the jth element in the second
array.

Can you explain what I'm missing? I'm not asserting anything; I'm sure
there's a perfectly good reason why this casting is not allowed. I'm
just not seeing it.

As for my original question, I'm simply wondering how to do the
following:

int** asdf = new int[i][j];

You can't apply the approach you suggested because you don't know the
size of the array in at compile-time.

"John Harrison" <jo*************@hotmail.com> wrote in
news:2q************@uni-berlin.de:

"overbored" <ov*********@SPAMoverbored.net> wrote in message
news:Xn******************************@127.0.0.1...
I can do this:

int asdf[2];
int* zxcv = asdf;

but not this:

int asdf[2][2];
int** zxcv = asdf;

or I get 'cannot convert from int[2][2] to int**'. Does anybody know
why this is, and how to get around it?


C++ says T[] converts to T*, it doesn't say anything about T[][] to
T** and why should it? It would be impossible to implement. T[]
converts to T* because x[i] is equivalent to *(x + i). But x[i][j] is
not equivalent to *(*(x + i) + j) because the pointer arithmetic
doesn't work.
I just want a pointer to that double
array. Thanks in advance.


Why? If you explain what you are trying to do then maybe someone can
tell you how.

You can do this

int asdf[2][2];
int (*zxcv)[2] = asdf;

Is that good enough?

john


Jul 22 '05 #3
Sorry, that was me.

Class Account <cs******@imail.eecs.berkeley.edu> wrote in
news:Xn**********************************@127.0.0. 1:

[snip]
Jul 22 '05 #4
"Class Account" <cs******@imail.eecs.berkeley.edu> wrote in message
news:Xn**********************************@127.0.0. 1...
I don't understand your explanation. Why do you say the pointer
arithmetic "wouldn't work"? Assuming x : int**,

x
+---+ +---+ +---+---+
| |---->| |---->| 5 | 7 |
+---+ +---+ +---+---+
| |
+---+
int** int* int
So,

x : int** returns the address of the 0th element of the first array.

x + i : int** returns the address of the ith element of the first array.

*(x + i) : int* returns the value of the ith element of the first array,
or the address of the 0th element in the second array.

*(x + i) + j : int* returns the address of the jth element in the second
array.

*(*(x + i) + j) : int returns the value of the jth element in the second
array.

Can you explain what I'm missing? I'm not asserting anything; I'm sure
there's a perfectly good reason why this casting is not allowed. I'm
just not seeing it.
A two-dimensional array's elements are stored contiguously, not in separate
arrays that are accessed via pointers that are stored contiguously. When
you access elements using brackets (e.g. [4][5]), you are actually using a
single (computed) offset from the first element. The same syntax for a
pointer-to-pointer, on the other hand, uses one offset to locate a pointer,
and then uses an offset from that pointer to locate the element desired.
They are quite different.
As for my original question, I'm simply wondering how to do the
following:

int** asdf = new int[i][j];

You can't apply the approach you suggested because you don't know the
size of the array in at compile-time.

<snip>

See the FAQ (http://www.parashift.com/c++-faq-lite/), Section 16 ("Freestore
management"), question 15 ("How do I allocate multidimensional arrays using
new?"). In fact, the whole section is a good read if you have questions
about dynamically allocating objects.

--
David Hilsee
Jul 22 '05 #5
> As for my original question, I'm simply wondering how to do the
following:

int** asdf = new int[i][j];


First things first:

int* a = new int[125];
is lain out in memory EXACTLY as is:
int (*b)[5][5] = new int[5][5][5];
Now, for some reason you want a pointer to a pointer to that array.

int** p_p_ints;

Okay, so in this variable, we need to store that address of a pointer. But
you don't have a pointer whose address to put in! Make one:

int** p_p_ints; //Pointer to pointer to int

int* p_ints; //pointer to int

p_ints = new int[5][5][5];

p_p_ints = &p_ints;
Now p_p_ints points to a pointer which points to your array of ints. And why
exactly do you want this?!
Once you've defined an array like:

int monkey[60];
there are ways of accessing it as if it were:
int monkey[2][30]

int monkey[3][20]

int monkey[4][15]

int monkey[5][12]

int monkey[6][10]
-JKop
Jul 22 '05 #6

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

Similar topics

5
by: Duke of Hazard | last post by:
I have a simple database like this: Agassi`6.0 Sampras`5.5 Tennis_novice`3.0 Tennis_beginner`2.0 I would like to sort it numerically by the second column so it looks like this:
2
by: Trimbitas Sorin | last post by:
Hello I have a simple syntax question : What does the following line mean: 1: %checkType; ?? I know that @test="" is an array and $test="" is a simple variable. Thank you With best regards...
3
by: Chan | last post by:
SqlParameter is a class, consider the following statements: 1> Dim Q1() as SqlParameter 2> Dim Q2 as SqlParameter() 3> Dim Q3() as SqlParameter()
5
by: Kitty | last post by:
Given a integer array. How can I know the total size of this array? Thanks.
6
by: Petar Popara | last post by:
I need some help converting byte into Array: int bufferLen = Convert.ToInt32(file.Length); byte buffer = new byte; int len = sr.Read(buffer, 0, bufferLen); ArrayList a = new ArrayList(len);...
5
by: Tim::.. | last post by:
Can someone tell me how I convert this simple SQL statement so I can use it in ASP.NET??? I have an issue with the quotation marks and wondered if there is a simple rule for converting the sql...
1
by: Agnes | last post by:
What is the different of the following (1)Dim myList() as string ={1,l2}...etc (2)Dim myList as array for (2), why can't just put the value in mylist(0) ??? e.g myLIst (0) =...
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.