Arrays are pointers when you create one.
Writing it with brackets [] or * doesn't matter. They basically means the same thing (for arrays).
You know what a pointer is?
A pointer points on an address in memory. In this example i made, the variable array points to an MyStruct array in memory.
So when we send the array to the function, we actually send the pointer, not the whole array.
I recommend always using pointer in the function when working with arrays:
int SearchA(MyStruct* array, int sID, const int SIZE);
No need to rep.
Extra:
You know the difference between an static array and a dynamic array?
A static array, declared like this:
or
Code:
const int SIZE = 5;
int arr[SIZE];
or
Code:
int array[] = {1,2,3,4,5};
* A static array cannot expand. If you define an static array to be the size 5, It will always be the size of 5.
* A static array requires a constant variable, when you declare the size.
* A static array is a pointer.
A dynamic array is declared like this:
Code:
int* arr = new int[10];
or
Code:
int size = 10;
int* arr = new int[size];
* A dynamic array can be expanded.
* A dynamic array is a pointer.
* Note that you don't require a constant to declare the size.
* You allocate memory to create a dynamic array. What you borrow, you have to give back, always. Other wise you get memory leaks.
To de-allocate memory, you simply write: delete [] arr; when you are finished with it.
* To expand a dynamic array, you simply write:
Code:
#include <iostream>
int main()
{
int capacity = 10;
int* arr = new int[capacity];
//Fill the array with something
for (int i = 0; i < capacity; i++)
{
arr[i] = 10+i;
}
//Now we say we want 10 more elements in the array, lets expand it
//create a temp pointer
int* temp;
//Allocate it with the new capacity
temp = new int[capacity*2];
//Copy the elements to our new array
for (int i = 0; i < capacity; i++)
{
temp[i] = arr[i];
}
//When we are done, deallocate our old arr, because its no longer needed.
delete [] arr;
//Point the arr pointer to our new array in the memory
arr = temp;
//arr now points on the same address as temp. We don't need temp anymore, lets point it to nothing
temp = NULL;
//increase the capacity
capacity = capacity * 2;
//Finished
delete[]arr;
return 0;
}