0

I have a pointer * double array and basically, they are indexed in any order.

For example,

double[0][0] = 80.0
double[3][0] = 56.8
double[4][0] = 56.7

How do I for example check to see if double[1][2] exists and then create one if it doesn't.

I do not intend on using vectors or anything 'fancy', just plain arrays.

6
  • 4
    With just plain arrays it can't. You need to keep track of the size somehow. Commented Dec 5, 2014 at 15:26
  • If you're plannig on doing this on an array in which the size is known, you could initializate all indexes with a default value and then proceed with a simple check. If you want to create a mutable array, which grows it's size dinamically, then I'd sugest using something 'fancier'. Commented Dec 5, 2014 at 15:29
  • Your best bet is to initialize the array with some sentinel value, a value that real data will never have. Then if(array[3][4]==SENTINEL) { and so on. Commented Dec 5, 2014 at 15:29
  • the full array sizing is created when the code declares the array. Anything after that is just filling in and reading the values. --or-- do you mean: how to determine if an array entry is already filled in? Commented Dec 5, 2014 at 15:41
  • 1
    The correct answer to this depends on whether you've declared array as double array[N][M] or double *array[N] or double **array. In the first case, as long as 0 <= x < N and 0 <= y < M, then array[x][y] exists (although it may be garbage if you've never initialized it). In the other cases, you'd have to explain how the allocations were done... Commented Dec 5, 2014 at 15:49

1 Answer 1

2

Hm. Well, if you really want to do it with plain arrays (why?), there's not much you can do but rely on a magic value. I'd suggest std::numeric_limits<double>::quiet_NaN() (NaN = not a number). That is to say:

#include <algorithm>
#include <limits>

double data[10][20]; // maximum dimensions mandatory with arrays. There's
                     // no way around that, and you can't grow it later.

std::fill(data[0], data[0] + 10 * 20, std::numeric_limits<double>::quiet_NaN());

Then you can later check if there's a real value in the array like so:

if(!std::isnan(data[1][2])) {          // #include <cmath> for std::isnan
  // oh, look, there's a value here!
} else {
  // Nothing here yet. Better fill it
  data[1][2] = 123.0;
}

Caveat: If your calculations may produce NaN values themselves, you're screwed this way. This happens, for example, if you attempt to calculcate 0.0 / 0.0 or std::sqrt(-1) or std::log(-1) or something else that has no defined value in the reals. If your calculations produce NaN and you write it into the array, this approach will act as though the value had never been written there.

Sign up to request clarification or add additional context in comments.

5 Comments

when you declare the array, you must have given it a size. For instance, 'double myArray[20][20];' so you know the largest offset will be myArray[19][19] (offsets start with 0 in C)
I'm a bit confused why you're telling me this.
@user3629249 you seem to refer to a comment I deleted meanwhile?
Oh, okay. I guess that makes sense.
@Wintermute I find the question horrible vague. You did get the pointer * double thing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.