Thread: notion confusion

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    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.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Damnit, I spelled notation in the subject wrong. Anyway, glad to see you're back, Prelude.

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.
    My best code is written with the delete key.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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.

  7. #7
    Cat
    Guest
    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).

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >...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.

    >I'm just saying that so you don't get any lawyers on me.
    I only call language lawyers on people.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By rits in forum C Programming
    Replies: 3
    Last Post: 03-12-2009, 08:00 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. for loop confusion
    By Enges in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2006, 08:21 AM
  4. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM
  5. Unicode - a lot of confusion...
    By Jumper in forum Windows Programming
    Replies: 11
    Last Post: 07-05-2004, 07:59 AM