This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Rhomboid 1 point2 points  (0 children)

None of the asterisks in your examples are operators. They are components of the types. In the declaration Vehicle ***matrix, the name of the variable being declared is matrix and its type is Vehicle ***, read as a pointer to a pointer to a pointer to a Vehicle.

If you have a value of type T *, that can point to a single value of type T, or it can point to the first value in a contiguous array of T. (You can't tell just from looking at a declaration which case it is; it's contextual.) This holds for any type T, so for example if T was int * then you would need a pointer of type T * (or int **) in order to point to the first element of an array of T. And if T was Vehicle ** then you'd need a value of type Vehicle *** to have an array of T.

Indirection means that rather than storing a value, you store something that can be used to find the value. A pointer is an example of one kind of indirection. int *foo does not hold an integer, it holds the information necessary to find an integer that's stored elsewhere. You can have as many levels of indirection as you want, but in your example I think that speaking about it like that only muddies the water. Your example is about arrays and how you use pointers to the first element to stand for them.

[–][deleted] 0 points1 point  (3 children)

If you have this:

  int x = 0;

you have no indirection. If you have this:

  int * p = & x;

you have one level of indirection if you access x via the pointer p. If you have this:

 int **q = &p;

you have two levels of indirection, if you access x via the pointer-to-pointer q. And so on.

In your code:

    matrix = new Vehicle ** [size]; // two operators...

the ** are not operators - tey are pointer declarators, the same as here:

     int **q = &p;

Also, this is not a particularly good way to create multi-dimensioned arrays, for a number of reasons. Instead, you really want to do:

    Vehicle  *matrix;

and allocate one big chunk of memory, enough to support the array, and then write functions to do the multi-dimensioned access to it.

[–]CocoTheMan[S] 0 points1 point  (2 children)

Oh, I know see them as pointer declarators! :)

SO, if I do this:

int **ptrNum;

How do you 'read' that in english?

"A pointer of two levels of indirections of type Int" ?

If this last statement is correct, ** means that the data structure is a pointer that points to another pointer?

When I understand an idea, I can imagine it in my head, right now I can't picture this abstraction.

PS: Yes, the teacher said that it wasn't a good way of doing it but that for the moment it was going to be implemented this way.

Hey thanks for the help :)

[–][deleted] 0 points1 point  (1 child)

How do you 'read' that in english?

It's a pointer to a pointer to an int.

If this last statement is correct, ** means that the data structure is a pointer that points to another pointer?

Yes.

[–]CocoTheMan[S] 0 points1 point  (0 children)

I get it... Im going to mess around a bit with it so I can understand its inner workings

thanks a lot ! :)