0

Hoping for a little C++ assistance - I'm very new to the topic. I'm attempting to dynamically create an array based on user input with a pointer, then pass the array to a function. But the pointer (and thus array) pass feels a little wrong because there is no dereferencing that occurs.

During/after passing, do we just treat the pointer as if it were any normally declared-and-passed array, without the need to dereference (*) anything? Or am I applying this incorrectly?

Pseudocode follows:

#include<iostream>
using namespace std;

void arrayFunc(int [], int);    // << Note no indication of pointer pass

int main()
{
    int *arrayPtr = 0; // Array pointer
    int arrayElem = 0; // Number of elements in array

    cout << "\nPlease enter the number of elements: ";
    cin >> arrayElem;

    arrayPtr = new int[arrayElem]; // Dynamically create the new array
    arrayFunc(arrayPtr, arrayElem); // << Note no dereferencing or other indication of pointer

    return 0;
}

void arrayFunc(int array[], int arrayElem)  // << Same here - now it's just a plain old array
{
    // All the functiony-bits go here, referencing array without the need to dereference
}

[EDIT] While the above code works, the following includes the fixes determined in the discussion below:

#include<iostream>
using namespace std;

void arrayFunc(int*, int);  // Changed to pointer pass instead of []

int main()
{
    int *arrayPtr = 0; // Array pointer
    int arrayElem = 0; // Number of elements in array

    cout << "\nPlease enter the number of elements: ";
    cin >> arrayElem;

    arrayPtr = new int[arrayElem]; // Dynamically create the new array
    arrayFunc(arrayPtr, arrayElem);

    return 0;
}

void arrayFunc(int* array, int arrayElem)   // Passing a pointer now instead of []
{
    // All the functiony-bits go here, referencing array without the need to dereference
}
3
  • 1
    Your pointer is a pointer to the first element of the array. Dereferencing it wouldn't allow you to pass in "the whole array". Commented Jul 10, 2015 at 15:54
  • Ahh, so, does arrayPtr just work like any normal array after the new int[arrayElem] statement? Commented Jul 10, 2015 at 15:57
  • 1
    It works the same way as if you had a pointer to the first element of a normal array (e.g., int arr[10]; int *ptr = arr;). It will work like an array for the most part, but not always (e.g., sizeof(arr) vs. sizeof(arrayPtr)). Commented Jul 10, 2015 at 16:00

3 Answers 3

1

You should pass the pointer in your function, because it describes the situation accurately i.e. you are passing a dynamically allocated memory. arrayPtr is essentially a pointer to the first element of the array. As a result, you do not need to worry about dereferencing it.

Change the function signature to:

void arrayFunc(int*, int);
Sign up to request clarification or add additional context in comments.

5 Comments

Ah interesting - so the prototype should indicate it's passing a pointer? What about the function header void arrayFunc(int array[], int arrayElem) { ... }? Should this be changed to void arrayFunc(int* array[], int arrayElem) { ... }?
You are passing a pointer, not an array. So, the function header should look like arrayFunc(int* array, int arrayElem) { ... }
Perfect - thank you! So, just to confirm, when we point to an array element there's no need to dereference (myIntArrayPtr[0] = 1;), but when we point to a non-array, such as an integer, we'd need to dereference (*myIntPtr = 1;)?
@WyattMiller when you use the square brackets [ ], you are dereferencing.
Aha! I didn't realize. Thank you!
1

Your attempt is correct. You are passing the array pointer by value. You can then dereference it as normal within arrayFunc

Comments

1

C is designed to pretend a pointer and an array are the mostly same thing. Lots of simple uses are easier because of that. But the concept gets much more confusing when you think about a pointer to an array. It feels like it shouldn't be the same thing as a pointer to the first element of that array, but in the common methods for allocating memory and using pointers, a pointer to an array really is just a pointer to the first element of the array.

I find it best to think of "pointer to first element of array of" as the normal meaning of * in C. The special case of pointing to a scalar object is effectively treating the scalar as the first (and only) element of an array of length 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.