# C++ question

**URL:** <https://boards.straightdope.com/t/c-question/166673>\
**Category:** Factual Questions\
**Created:** [April 5, 2003, 12:50pm UTC](https://boards.straightdope.com/t/c-question/166673 "2003-04-05T12:50:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scarf-Ace](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Scarf-Ace](https://boards.straightdope.com/u/Scarf-Ace)\
**Post date:** [April 5, 2003, 12:50pm UTC](https://boards.straightdope.com/t/c-question/166673/1 "2003-04-05T12:50:10Z")

</div>

Sorry for posting here, but not getting an answer anywhere else.

My prob is dynamically allocating a 2D array:

Thing::Thing(int num1, int num2)  
{  
ptr = new float[num1][num2];  
}

where ptr is defined as;

float \*\*ptr;

This is not allowed, so what syntax would I use to do this? I want to be able to reference the array using \*[j] and not fiddle around with pointer notation.

---

<div class="post-metadata">

**Author:** ![Garnet](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@Garnet](https://boards.straightdope.com/u/Garnet)\
**Post date:** [April 5, 2003, 1:55pm UTC](https://boards.straightdope.com/t/c-question/166673/2 "2003-04-05T13:55:50Z")

</div>

Unfortunately the only way I know of is a bit ugly…  
Try this:

Thing::Thing(int num1, int num2)  
{  
ptr = new float \*[num1];  
for (int x = 0; x \< num1; x++) ptr = new float[num2];  
}

And you’ll need similar code in the destructor to clean up the memory afterward. But this will let you use ptr\*[j] type notation, I believe.

I hope this helps.

---

<div class="post-metadata">

**Author:** ![Shade](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Shade](https://boards.straightdope.com/u/Shade)\
**Post date:** [April 5, 2003, 3:33pm UTC](https://boards.straightdope.com/t/c-question/166673/3 "2003-04-05T15:33:42Z")

</div>

For this sort of thing I often try searching google groups. Someone’s always asked the same q before, if you can find it. Sorry I can’t be more help.

---

<div class="post-metadata">

**Author:** ![Shade](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Shade](https://boards.straightdope.com/u/Shade)\
**Post date:** [April 5, 2003, 3:35pm UTC](https://boards.straightdope.com/t/c-question/166673/4 "2003-04-05T15:35:14Z")

</div>

Or maybe have an array of classes, each of which implements an array?

---

<div class="post-metadata">

**Author:** ![ZenBeam](https://avatars.discourse-cdn.com/v4/letter/z/3ab097/32.png) [@ZenBeam](https://boards.straightdope.com/u/ZenBeam)\
**Post date:** [April 5, 2003, 4:23pm UTC](https://boards.straightdope.com/t/c-question/166673/5 "2003-04-05T16:23:37Z")

</div>

You can use the Numerical Recipes codes, nrutil.h and nrutil.c, [available here](http://www.nr.com/public-domain.html). These are public domain, and implement what **Garnet** described, more-or-less.

---

<div class="post-metadata">

**Author:** ![sturmhauke](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@sturmhauke](https://boards.straightdope.com/u/sturmhauke)\
**Post date:** [April 5, 2003, 4:36pm UTC](https://boards.straightdope.com/t/c-question/166673/6 "2003-04-05T16:36:27Z")

</div>

As far as I know Garnet’s answer is the simplest. One thing to remember is that the syntax

float \*\*ptr;

creates a pointer to a pointer to a float, whereas what you need is a pointer to an array of pointers to arrays of floats.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [April 5, 2003, 5:28pm UTC](https://boards.straightdope.com/t/c-question/166673/7 "2003-04-05T17:28:32Z")

</div>

> [@](#):
>
> \*Originally posted by sturmhauke \*  
> \*\*As far as I know Garnet’s answer is the simplest. One thing to remember is that the syntax
> 
> float \*\*ptr;
> 
> creates a pointer to a pointer to a float, whereas what you need is a pointer to an array of pointers to arrays of floats. \*\*

If you want a 2D array of objects of type T, you declare it as T\*\* Array. If you want pointers of type T, you declare it as T\*\*\* Array.

**Garnet** ’s initialization is the only way I know. Gets to be a bitch when you’ve got more than three or four dimensions.

---

<div class="post-metadata">

**Author:** ![Scarf-Ace](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Scarf-Ace](https://boards.straightdope.com/u/Scarf-Ace)\
**Post date:** [April 6, 2003, 1:04pm UTC](https://boards.straightdope.com/t/c-question/166673/8 "2003-04-06T13:04:04Z")

</div>

You know I tried Garnet’s method and I got a segmentation fault, i’d better have a closer look at my code.

As for using a library - hell is other peoples code.
