POINTERS TO ARRAY

This is a class exercise in which I am graded, if you do not feel like answering it I will understand. On the other hand I am lost and can't figure out what I am doing wrong, so hint is acceptable to me.

The exercise is in form of a sentence, no variable declarations are given we
have to figure it out ourselves.
I have this sentence declaration #1:

aValue is a pointer to a pointer to an array of 3 floats. which I converted to
float (**aValue)[3];

and this is sentence declaration #2:
4. myValue is a reference to a pointer to a pointer to an array of 3 floats. Initialize myValue to aValue.

I converted myValue into.
float (**&myValue)[3];

I know if the sentence declarations are not arrays like this:
float (**aValue)

float (**&myValue)

I can do this assignment
float (**&myValue) = aValue;

But when the variable is declared as an array:
Like this:

float (**aValue)[3]

float (**&myValue)[3]
I can not do this assignment, and all I did is put the [3] to make it an array
float (**&myValue)[3] = aValue[3];


what am I doing wrong.

Ed
You have a cruel teacher! It looks like you are creating an array of 3 references, not a reference to a single array. There may be a syntax to solve this in one line, but my first thought is to break it up as a typedef; its clearer for both you and any future readers. Typedef the type of aValue, then the reference portion becomes trivial. I won't give it to you, but you should be able to figure it out from that - if you have trouble, ask again with what you have and help will be given :)
First create a typedef for an array of 3 float.

1
2
3
4
5
6
7
8
9
10
11
12
typedef float array_type[3] ;
typedef array_type* pointer_to_array_type ;

array_type array = { 1.2f, 3.45f, 6.7890f } ;
pointer_to_array_type pointer = &array ;

// aValue is a pointer to a pointer to an array of 3 floats.
pointer_to_array_type* aValue = &pointer ;

// myValue is a reference to a pointer to a pointer to an array of 3 floats.
// Initialize myValue to aValue.
pointer_to_array_type*& myValue = aValue ;


Rollie,

1. Cruel teacher, you maybe right, but he is good. His exercises are hard but were designed to make you think. what I mean is think hard. He teaches at the University of California San Diego and works as a civilian programmer at Space and Naval Warfare Systems Command (SPAWAR). Just Imagine how much we learn from him.

2. BTW, The good part is we only have 4 exercises a week and this I just posted is the easy one. This question is one of five question of exercise 2 . You should have seen exercise 3 and 4.

3. Anyway, his exercise specification is: The solution must be one line of code. so breaking it down in pieces is not a solution to him.

Anyway thanks for looking, I was expecting anybody who glance at the question will immediately close it.
Cruel teachers are often the best :)

One line you say? Slightly different than 1 expression! (especially when declaring 2 variables - the entire program can be 1 line if you really wanted)

Okay, so 1 expression, if he doesn't specify a c++ version, how about:
1
2
float (**aValue)[3];
auto & someValue = aValue;


or, similarly:
 
decltype(aValue) & theValue = aValue;


What I'm sure he's looking for is this tho:
 
float (** (&theValue))[3] = aValue;
Last edited on
What mendozae already has is fine: float (**&myValue)[3] = aValue;

This would compile: float (*&myValue2)[3] = aValue[3] ;.
Though it would lead to undefined behaviour - aValue is not an array.

And this would not even compile: float (**&myValue3)[3] = aValue[3] ;
Rollie, JLBorges;

I went back to my program and read the sentence and my translation.

It was stupid me who made a mistake.
This is what I posted on the forum

aValue is a pointer to a pointer to an array of 3 floats. which I converted to
float (**aValue)[3];

but when I ran my test I entered in visual studio like this. No parenthesis

float **aValue[3]; in a sentence means. aValue is an array of 3 pointers to a pointer to a float. which is different from the original sentence.

so both of you were right:
float (**&myValue)[3] = aValue; is the right answer.


rollie,
You maybe right cruel teachers are often the best. Let me tell you more about this guy. when we write the code:

1. if the tab is 3 space, it has to be 3, 6, 9 spaces increment on the tab.
if the tab is 4 space, it has to be 4, 8, 12 spaces increment. when you tab it
has to show as spaces in the program and not tabs, his reason the program
has to be transportable to another compiler and machine.

2. If he sees a MAGIC NUMBER in your code, to him that is a violation of a proper programming technique and it is a zero

3. He uses a software called GIMPEL PC-LINT that our program should pass, aside from passing the compiler. At the first 2 weeks of the class somebody complained and said that he took his class to learn programming and not how to edit the source code. And he said you will learn how to write program correctly, that will pass the government specification and can be run from any machine and any compiler.


This is the kind of instructor this country needs.
Topic archived. No new replies allowed.