Thread: Please explain..

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    68

    Please explain..

    Could someone please explain what the correct answer is?

    We have

    Code:
    double* myArray;
    declared in main.

    An array is dynamically allocated in a function called "createArray" Which function declaration would be THE BEST for "createArray"?
    Code:
    void createArray(double* array, int size);
    void createArray(double** arrays, int size);
    void createArray(double& array, int size);
    void createArray(double array, int size);

    Could someone explain WHY they got the answer that they did? This was on a test, and I still can't understand why the answer is (what it is). I don't want to publish his/her answer until i get a couple of the same answers. Thanks for the help.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    You have to pass the address of the pointer ("&myArray") to the function. So, the argument is of type "double**" (the address to some address).

    If you just passed the pointer ("myArray"), i.e. using your first function from the list, then the function "overwrites" its value, and the caller basically "lost" the pointer; also this would be a memory leak.

    It doesnt make sense to pass the value of the pointer ("*myArray"), i.e. using your last function from the list, because that doesnt achieve anything. I dont know how to explain this one, hopefully its obvious! Its just passing a "double", like "createArray( 1.0, size)", which doesnt make sense.

    I never use the syntax for the 3rd one, and I dont even think its correct.

    This leaves the second function, "double**". As mentioned above, the caller calls this function by passing the address of the pointer, i.e.:
    Code:
    double* myArray;
    createArray(&myArray);
    The function then doesnt modify this argument's value (if it did, the caller would "loose" it, as above), rather it modifies the value at this value's address (i.e. *myArray). So createArray uses malloc to create the dynamic array, and assigns it to the value pointed to by the pointer, i.e.:
    Code:
    void createArray(double** array, int size)
    {
      *array = malloc( sizeof(double) * size); // go to the value of "array" (i.e. the address of myArray), and save the address of this new array at its value (i.e. myArray = this new array)
    }
    Now the caller still "knows" where this new array was created, because the address of "myArray" wasnt changed, only its value. And its value is now the (first index of) the array created by createArray.

    Hope its clear... try and draw out a picture of whats going on, one for each of your 4 cases, to see whats happening, and cancel out the ones that dont work. Let us know if you have questions.

    EDIT: The reason you have to do it the above way, is because your changing the pointers value, i.e. giving it a different piece of memory... which means you have to pass its address, so you still know where it is. If you werent changing its value, such as printing the values of the array, it would be straightforward and you could just pass the pointer, i.e.
    Code:
    void printArray(double * array, int size)
    {
      // iterate over and print
    }
    
    int main()
    {
      double * array;
      // create and fill up array
      printArray(array);
    }
    A similar thing would be done if you were manipulating its contents, such as reversing the values in the list. Doing something like "array[0] = array[10]" is quite different from doing "array = // new address".
    Last edited by nadroj; 10-30-2009 at 11:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  2. One Easy" C " Question. Please Solve and Explain.
    By RahulDhanpat in forum C Programming
    Replies: 18
    Last Post: 03-24-2008, 01:39 PM
  3. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  4. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 09:53 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM