-
notion confusion
What is this called?
Code:
void name(float *array);
And what is this called?
Code:
void name(float array[]);
I figured the first one is called passing an array to a function by pointer notation and the second one is called passing an array to a function by subscript notation.
-
>I figured the first one is called passing an array to a function by pointer notation
>and the second one is called passing an array to a function by subscript notation.
That's how I describe it. Of course, you also have to be aware that both notations as the formal parameters to a function are equivalent. The compiler always sees pointer notation even if the source code shows array notation. As such, which you use is purely a matter of style.
-
Damnit, I spelled notation in the subject wrong. Anyway, glad to see you're back, Prelude. :)
-
By the way, does anyone know of any place on the net that explains the pointer notation and subscript notation names in function parameters? My book says they're only called that in the function definition. Yeah, I know, I have a crappy book.
-
I'm not sure I understand what you mean concerning 'names', but this is a good description of the relationship between pointers and arrays. Here is another that doesn't go into arrays and function parameters, but the general idea is that an array (when used as a parameter to a function) is converted to a pointer to the first element in the array.
-
I'm just asking because when I put this in my program...
Code:
void display_array( int array[ ], int size )
{
for ( int i = 0; i < size; i++ )
cout << array[ i ] << endl;
cout << endl;
}
...my instructor said it's wrong. He said it should be...
Code:
void display_array( int *array, int size )
{
for ( int i = 0; i < size; i++ )
cout << array[ i ] << endl;
cout << endl;
}
He told me to use subscript notation, so I thought he would accept array[] instead of *array in the function's parameter. He said that subscript notation only applies inside the definition of the function. So, I've been trying to find a website that backs him.
Though, you should know he's about 70 years old, he always uses void main(), and he still uses <iostream.h>. I'm not sure exactly how he's qualified to teach C++ since he doesn't even have a degree in computer science; he has a degree in business and economics.
Also, Prelude, your whitespace coding style is very helpful for understanding code - a very nice style. I'm just saying that so you don't get any lawyers on me. :D
-
Well, in a way, using the pointer is more clear; the two functions you wrote generate exactly equivalent code. It makes it a little more clear to use the pointer as the parameter, because the actual variable that is pushed to the stack is the address of the first array element (i.e. a pointer).
-
>...my instructor said it's wrong.
Your instructor is misinformed (note the nice diplomacy). The two are exactly the same; array notation decays into pointer notation.
>He said that subscript notation only applies inside the definition of the function.
Subscript notation refers to the use of the [] operator to index a contiguous block of items in memory using an offset. Your instructor is probably talking about the actual process of indexing an array where
a[n]
is syntactic sugar for the real work being performed
*(a + n)
In a way he's correct, though none of the standard or authoritative works specify 'array notation' or 'pointer notation'. Array notation is taken to mean anything relating to an array including declarations and formal function parameters. Likewise, pointer notation is taken to mean anything using the address-of or indirection operators. Your mileage may vary, which is clearly the source of this thread. :)
>he always uses void main(), and he still uses <iostream.h>
I highly recommend you verify everything he teaches you. If he's too sloppy to bother properly learning the language he teaches, he's probably sloppy everywhere else too.
>Also, Prelude, your whitespace coding style is very helpful for understanding code - a very nice style.
Don't say that, I've slowly been dropping it in favor of something more conventional. Now you'll make me regret the change. :D
>I'm just saying that so you don't get any lawyers on me.
I only call language lawyers on people. ;)