pointers.

This is a discussion on pointers. within the C++ Programming forums, part of the General Programming Boards category; i've been doing revision on pointers, and....well i'll show you the codes. in one function i've got it declared as: ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    pointers.

    i've been doing revision on pointers, and....well i'll show you the codes.

    in one function i've got it declared as:

    int findIndexMin(int *arr, ........)
    and thats an array, however inside of that function when refferring to the array, it only uses "arr" without the "*".

    However in a different code:
    void UpdatePurchase(float *accountPtr........)
    inside of this function, anything that is referring to the account pointer is shown as "*accountPtr" with the "*" there.

    Is there a reason for this??

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Yup

    When you pass an array to a function you are passing a pointer to it's initial argument:

    int findIndexMin(int *arr)
    int findIndexMin(int arr[])

    Both are in fact about the same thing. The thing to remember here is that on the second case even though you are not using pointer notation, the array will still be converted to a pointer.

    Now the tricky part is that an array that was passed as an argument to a function need not be used with pointer notation (no matter what the above methods you used). It's is my opinion that for the sake of consistency the first form above is the most correct one (after all, the array will be converted to a pointer no matter what). It is also my opinion that inside the function you should be then using pointer notation when referring to the array.

    The authour of your code choose to use pointer notation for the header of the function and not for the body. This is fine and a property of the arrays as I described above. But it's not logically consistent with proper pointer grammar.

    Just a final note: When you pass a pointer as an argument something change with the usual pass by value mechanism. And this obviously as a strong effect on arrays as they are converted to pointers wether you wish or not.

    Pass by value, as you know, creates a copy of an argument and uses that copy throughout the function body, leaving the original unchanged. But when you pass a pointer you are indeed passing the memory address of that variable. So, it still creates a copy... but a copy to a memory address. This means the copy still points to the original variable. So when changing an element of the copy array, you will indeed be changing the original element. IF you wish to enforce the fact that you are not going to change the array you can declare the argument in the following manner:

    int findIndexMin(const int *arr)
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 11:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21